Package: django-ext-utils
Django-ext-utilsUseful utilities for your django projectRequirementsPython >= 3.0Django >= 2.0Pillow(optional) DjangorestframeworkInstallationpip install django-ext-utilsAdd to your settingssettings.pyINSTALLED_APPS=['django.contrib.admin','django.contrib.auth',...'django_ext_utils',]SERIALIZERS_PATH='api.v1.serializers'# path to serializers folderUsageResizeImageMixinResize image on the fly before save toImageField:fromdjango.contrib.auth.modelsimportAbstractUserfromdjango_ext_utils.utilsimportResizeImageMixinclassUser(AbstractUser,ResizeImageMixin):avatar=models.ImageField(upload_to='avatars')...defsave(self,*args,**kwargs):ifself.pkisNone:self.resize(self.avatar,(200,200))# here!super().save(*args,**kwargs)DeletedModelMixinOndeletemarkis_mark_as_deleteinsteadfromdjango_ext_utils.utilsimportDeletedModelMixinSingletonModelMixinCreate only one instance of modelfromdjango_ext_utils.utilsimportSingletonModelMixinServiceService-form-oriented helper (I really like services)fromdjango_ext_utils.utilsimportServicefrom.modelsimportSubscription# Services/service.pyclassAddSubscriptionService(Service):defgo(self):email=self.cleaned_data.get('email')name=self.cleaned_data.get('name')subscription,created=Subscription.objects.get_or_create(email=email,defaults={'name':name})ifcreated:# notify user......returnsubscription# views.pysubscription=NotificationService.exec({'email':'foo@example.com','name':'foo'})...returnResponse(subscription.serialize_data)Rest api helpersResponsesMixinwrapper forrest_framework.Responsewith statusessimple_text_responsereturn text with200codesuccess_objects_responsereturn object with200codeerror_responsereturnErrorobject with400codeclassUserProfileViewSet(ResponsesMixin,GenericAPIView):serializer_class=UserSerializerdefpost(self,request,*args,**kwargs):user=request.userserializer=UserSerializer(instance=request.user,data=request.data,context={'request':request})ifserializer.is_valid():serializer.save()returnself.success_objects_response(serializer.data)# herereturnself.error_response(serializer.errors)# heredefget(self,request,*args,**kwargs):user=request.userdata=user._serialize_data(request)returnself.success_objects_response(data)# hereBaseAbstract class to help serialize your modelsget_serializer # Return serializer by name (ModelSerializer)get_short_serializer # Retrun another serializer by name (ModelShortSerializer)serialize_data # Find serializer by name and returnserialize data_serialize_data(request)  # Find serializer by name and serialize data withrequestIMPORTANT: you must create serializer by rule:ModelNameSerializer orModelNameShortSerializerfromdjango_ext_utils.rest_utilsimportBase# models.pyclassUser(AbstractUser,Base,ResizeImageMixin):avatar=models.ImageField(upload_to='avatars')....# serializer.pyclassUserSerializer(serializers.ModelSerializer):classMeta:model=Userfields=['pk','first_name','last_name']# views.pyfromrest_framework.genericsimportGenericAPIViewclassUserApiView(GenericAPIView):defget(self,request,*args,**kwargs):user=request.userdata=user._serialize_data(request)# here!returnself.success_objects_response(data)ImageBase64FieldWrapper forserializers.ImageField()Gotbase64string and save in modelImageFieldReturnlink to image(http://example.com/media/avatars/my.jpeg)# serializer.pyclassSomeUserApiView(serializers.ModelSerializer):image=ImageBase64Field()created_at=TimestampField(read_only=True)classMeta:model=Userfields=['uid','image','created_at']