What are mixins in DetailView and unable to understand what happened between 6:30-7:30

what are mixins in DetailView and unable to understand what happened between 6:30-7:30

Hello @avin99,
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 the mixin.
So take it exactly as using the multiple inheritances. 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 a simple example of a 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 rest framework.
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 eg. in this we are using SimpleObjectMixin and the post function is predefined in this mixin and we are inheriting 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 multiple objects.
And we can also use this in Django rest framework.
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. Feel free to ask if you face any doubt or difficulty.
Thanks :slight_smile:
Happy Coding !!

@A17LP0026 sir, after changing name=“choice” in questioni.html still getting error, what’s the solution? please help.

Pls tell me the exact duration where it happened ?