![]() ![]() 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:.If I rename the fields in the model ( s/attributes/foos/g, s/skills/bars/g) then I get a different (less clear?) error : Improperl圜onfigured at /characters/api/magesįield name `attributes` is not valid for model `ModelBase`. GenericRelatedObjectManager object at 0x00000000051CBD30> is not JSON serializableĭjango Rest Framework thinks I want to serialize my generic relationship. Which means I get this error: TypeError at /characters/api/mages Skills = GenericRelation('CharacterSkillLink') ![]() Player = models.ForeignKey('auth.User', related_name="%(class)s_by_user")Īttributes = GenericRelation('CharacterAttributeLink') The issue is on the model I have this: class NWODCharacter(models.Model): I'd like my Mage Serializer to have these 'mixed in' rather than c/p then between all types of character (Mage is a type of character) hopefully this will increase DRYness in my webapp. GenericCharacterFieldMixin is a Mixin of Fields for Characters, that are Generic, i.e. 'power_level', 'energy_trait', 'virtue', 'vice', 'morality', 'size', Skills = serializers.SerializerMethodField('character_skill')Ĭharacter_attribute_fields = įields = ('id', 'player', 'name', 'sub_race', 'faction', 'is_published', I have this: class GenericCharacterFieldMixin():Īttributes = serializers.SerializerMethodField('character_attribute') ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |