![]() ![]() UpdateModelMixin provides update and partial_update actions.RetrieveModelMixin provides a retrieve action.partial_update ( request, * args, ** kwargs ) def delete ( self, request, * args, ** kwargs ): return self. update ( request, * args, ** kwargs ) def patch ( self, request, * args, ** kwargs ): return self. retrieve ( request, * args, ** kwargs ) def put ( self, request, * args, ** kwargs ): return self. all () def get ( self, request, * args, ** kwargs ): return self. DestroyModelMixin, GenericAPIView ): serializer_class = ItemSerializer queryset = Item. In this example, we combined all three into a single endpoint for every action that's possible for a detailed view:įrom rest_framework import mixins from rest_framework.generics import GenericAPIView class RetrieveUpdateDeleteItem ( mixins. You can use one of them or combine them as you see fit. RetrieveModelMixin and UpdateModelMixin both return serialized representations of the object, while DestroyModelMixin, in the case of a success, returns HTTP_204_NO_CONTENT. RetrieveModelMixin, UpdateModelMixin and DestroyModelMixin all deal with a single model instance. RetrieveModelMixin, UpdateModelMixin and DestroyModelMixin The sole purpose of this was to show you how binding methods and actions work. This will produce the following results.ĭISCLAIMER: Just because this is possible doesn't mean I advise you to do it. create ( request, * args, ** kwargs ) def post ( self, request, * args, ** kwargs ): return self. CreateModelMixin, GenericAPIView ): serializer_class = ItemSerializer queryset = Item. Here's an example of what a mixin looks like:įrom rest_framework import mixins from rest_framework.generics import GenericAPIView class CreateList ( mixins. You can use only one of them or combine them to achieve the desired result. While the mixin classes provide create/retrieve/update/delete actions, you still need to bind the appropriate actions to the methods. They cannot be used standalone they must be paired with GenericAPIView to make a functional view. Since there are several mixins meant to be used with GenericAPIView, I don't recommend reinventing the wheel by using it on its own. Alternatively, you can overwrite get_queryset()/ get_serializer_class(). When extending GenericAPIView, queryset and serializer_class must be set. delete () return Response ( status = status. data ) def delete ( self, request, * args, ** kwargs ): instance = self. get_serializer ( instance ) return Response ( serializer. all () def get ( self, request, * args, ** kwargs ): instance = self. Although it's designed to be combined with mixins (as it's used within generic views), it's possible to use it on its own:įrom rest_framework.generics import GenericAPIView from rest_framework.response import Response class RetrieveDeleteItem ( GenericAPIView ): serializer_class = ItemSerializer queryset = Item. It provides methods like get_object/ get_queryset and get_serializer. GenericAPIView is a base class for all other generic views. When using generic views, concrete views is probably the level you'll be working with. Generic views is a word that represents both mixins and concrete views. ![]() Since the names are so similar, it's easy to confuse them. In the documentation and in code comments, they can be found as concrete view classes, concrete generic views or concrete views. Concrete views combine GenericAPIView with the appropriate mixins to create views often used in APIs.ĭRF uses different names for concrete views.It isn't really useful on its own but can be used to create reusable actions. GenericAPIView is a more loaded version of APIView.They consist of GenericAPIView, mixins, and concrete views: Their purpose is for you to quickly build API views that map closely to your database models without repeating yourself. They're built on top of the APIView class, which we presented in the previous article of this series. Generic views are a set of commonly used patterns. Which generic views you can use and what they do.How to create a custom mixin and how to use it.Which mixins you use and what they can do.What GenericAPIView is, what mixins are, and how they both come together to create concrete views.RetrieveModelMixin, UpdateModelMixin and DestroyModelMixinīy the end of this article, you should be able to explain:. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |