Package: drf-serialization-magic
DRF Serialization MagicA collection of useful Decorators to DRY up your Django Rest Framework project when working with SerializersFull documentation onread the docsOverviewSerialization magic helps you boost your code and reduce duplication
by using a higher-order function to reduce the number of lines of code
and maintain consistency in your project.RequirementsPython (>= 3.7)Django(>= 3.0)Django REST Framework(> 3.11)InstallationUsingpip:$pipinstalldrf-serialization-magicBasic UsageRenderSerialization# model userfromdjango.contrib.auth.modelsimportAbstractUserclassUser(AbstractUser):pass# serializers.pyfromrest_frameworkimportserializersfromdrf_serialization_magic.modelsimportUserclassUserInformationSerializer(serializers.ModelSerializer):email=serializers.EmailField()username=serializers.CharField()classMeta:model=Userfields=["id","email","username"]With@api_viewdecorator functionfromrest_framework.decoratorsimportapi_viewfromdrf_serialization_magicimportRenderSerializationfromdrf_serialization_magic.serializersimportUserInformationSerializer@api_view(http_method_names=["GET"])@RenderSerialization(serializer_cls=UserInformationSerializer)defget_user_func_view(request):returnrequest.userAPI response{"data":{"id":1,"email":"userA@example.com","username":"userA"}}WithGenericViewSetclassfromdrf_serialization_magicimportRenderSerializationfromrest_framework.viewsetsimportGenericViewSetfromdrf_serialization_magic.modelsimportUserfromdrf_serialization_magic.serializersimportUserInformationSerializer# ...classUserViewSet(GenericViewSet):defget_queryset(self):returnUser.objects.all()@RenderSerialization(serializer_cls=UserInformationSerializer)deflist(self,request):returnself.get_queryset()API response{"count":2,"next":null,"previous":null,"data":[{"id":1,"email":"tom_hiddleston@gmail.com","username":"tom_hiddleston"},{"id":2,"email":"chris_hemsworth@gmail.com","username":"chris_hemsworth"}]}WithAPIViewclassfromrest_framework.viewsimportAPIViewfromdrf_serialization_magicimportRenderSerializationfromdrf_serialization_magic.serializersimportUserInformationSerializerclassUserAPIView(APIView):@RenderSerialization(serializer_cls=UserInformationSerializer)defget(self,request):returnrequest.userValidateSerializationWhen validating body data(POSTmethod)fromrest_framework.viewsimportAPIViewfromdrf_serialization_magicimportRenderSerialization,ValidateSerializationfromdrf_serialization_magic.serializersimportUserInformationSerializerfromdrf_serialization_magic.modelsimportUser# combination usageclassUserAPIView(APIView):@RenderSerialization(serializer_cls=UserInformationSerializer)@ValidateSerialization(serializer_cls=UserInformationSerializer,location="body")defcreate(self,request,data):# data object is validated# the default will use key `data`, if you want to use another key,# pass argument to_key="something" in ValidateSerializationuser=User.objects.create(**data)returnuserAPI callPOST/api/usersHTTP/1.1
Host:127.0.0.1
Content-Type:application/json
Accept:*/*{"email":"chris.evans","username":"chris_evans"}API response (422 HTTP status's code){"errors":[{"field":"email","detail":["Enter a valid email address."]}],"message":"Validation Error!"}When validating query paramsfromdrf_serialization_magicimportRenderSerialization,ValidateSerializationfromdrf_serialization_magic.serializersimportUserInformationSerializerfromdrf_serialization_magic.modelsimportUserfromrest_framework.viewsetsimportGenericViewSetclassUserViewSet(GenericViewSet):# combination usage@RenderSerialization(serializer_cls=UserInformationSerializer)@ValidateSerialization(serializer_cls=UserInformationSerializer,location="query")deflist(self,request,query):# data dict is validated# the default will use key `data`, if you want to use another key,# pass argument to_key="something" in ValidateSerializationuser=User.objects.filter(username=query["username"])returnuserAPI callGET /api/users?email=chris_evans HTTP/1.1API response(422 HTTP status's code){"errors":[{"field":"email","detail":["Enter a valid email address."]}],"message":"Validation Error!"}Accept list query params lookup:fromdrf_serialization_magicimportRenderSerialization,ValidateSerializationfromdrf_serialization_magic.serializersimportUserListLookUpSerializer,UserInformationSerializerfromdrf_serialization_magic.modelsimportUserfromrest_framework.viewsetsimportGenericViewSetclassUserViewSet(GenericViewSet):# combination usage@RenderSerialization(serializer_cls=UserInformationSerializer)@ValidateSerialization(serializer_cls=UserListLookUpSerializer,location="query")deflist(self,request,query):# query dict is validated# the default will use key `query`, if you want to use another key,# pass argument to_key="something" in ValidateSerializationuser=User.objects.filter(username__in=query["username"])returnuserAPI requestGET /api/users?username=chris_evans&username=tom_hiddleston HTTP/1.1API response{"count":2,"next":null,"previous":null,"data":[{"id":1,"email":"tom_hiddleston@gmail.com","username":"tom_hiddleston"},{"id":3,"email":"chris.evans@gmail.com","username":"chris_evans"}]}TestingTested with:Python (>= 3.9)Django(>= 3.1)Django REST Framework(> 3.11)SupportIf you need help, don't hesitate to start anissue.
For commercial support, please contact via email:Thang Dang Minh