Reagarding mixin

I do not understand what mixin basically does…
In this case , the example of singleobjectmixin is taken.So, please explain to me about mixin,how does it work and also about this singleobjectmixin …

Mixins provide more functionality. It is generally helpful when you are writing all the views of the same type. Like there are 4 to 5 views and in all of them you are calling the same object and then accessing the particular info. In that case you can create an object mixin and you can call that to the other parts of the code so that we can use the same. You don’t need to write the same thing again and again just write them in mixin.

Can you explain to me with better examples so that i can get a firm hold on mixins and how to use them in django?

Really really sorry Munish, for the inconvenience I forgot about this doubt.
So the mixins are used to provide the extra functionality as I said last time.
So take it exactly as using the multiple inheritence. You have to pass the data or the request as well as the mixin at the argument.

class Door(object):
	pass

class LockedDoor(Door):
	pass

class ShortDoor(Door):
	pass

class MagicDoor(Door):
	pass

class LockedShortMagicDoor(LockedDoor,ShortDoor,MagicDoor):
	pass

This is the simple example of mixin. Now in django some of the mixins are predefined and inherits some of the property on the basis of the object
type and the mixin type. We can also use mixin in django restframework.
Mixins are one of the main reason for providing the power to the class based views. And while passing the arguments generally we pass the mixins first then the other
arguments. There are various types of mixins like the ContextMixin, TemplateResponseMixin, SingleObjectMixin, MultipleObjectMixin so all these are predefined
in django.
Munish, simply consider mixins are the classes which the developer has built for us in advance with some of the attributes and methods so that we can save our time and don’t need to
write the whole functionality again.

For ex. in this we are using SimpleObjectMixin and the post function is predefined in this mixin and we are inherting this post method of the SimpleObjectMixin so that we can record the data
and for recording something we need to use the POST request so as to save the data.

from django.http import HttpResponseForbidden, HttpResponseRedirect
from django.urls import reverse
from django.views import View
from django.views.generic.detail import SingleObjectMixin
from books.models import Author

class RecordInterest(SingleObjectMixin, View):
    model = Author

    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return HttpResponseForbidden()
        self.object = self.get_object()
        return HttpResponseRedirect(reverse('author-detail', kwargs={'pk': self.object.pk}))   

So here SingleObjectMixin is related to a particular object of the class Author and having the data for that particular object. Similarly MultipleObjectMixin is used for the multiple objects.
And we can also use this in django restframework.
Like

class BlogPostAPIView(generics.ListAPIView):
	lookup_field = 'pk'
	serializer_class = BlogSerializer

	def get_queryset(self):
		return BlogPost.objects.all()

# So this is the basic Listview but if we want to add the functionality of creating objects also then we can use CreateModelMixin


class BlogPostAPIView(mixins.CreateModelMixin,generics.ListAPIView):
	lookup_field = 'pk'
	serializer_class = BlogSerializer

	def get_queryset(self):
		return BlogPost.objects.all()

	def post(self,request,*args,**kwargs):
		return self.create(request,*args,**kwargs)	
# so here the post method is predefined under the CreateModelMixin

Now I hope you get some idea of the mixins. Again I am really really sorry I forgot about this doubt. Now I will stick to this post pls let me know if anything is unclear to you I will try to respond as soon as possible. Or you can contact me over [email protected]
Thanks :slight_smile:
Happy Coding !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.