Package: django-rest-structure
Django Rest StructureThis app make your rest api application simpleDocument1. Install by runningpip install django-rest-structure2. Add middleware to yourMIDDLEWARE(orMIDDLEWARE_CLASSES) setting like this:MIDDLEWARE=('django_rest_structure.middlewares.middleware.RequestHandlerMiddleware',...)3. Add exception handler to yourREST_FRAMEWORKsetting like this:REST_FRAMEWORK={...'EXCEPTION_HANDLER':'django_rest_structure.results.exception.exception_handler'...}then add toYOUR-PROJECT/urls.pyhandler404='django_rest_structure.api.views.error_view_404'handler500='django_rest_structure.api.views.error_view_500'handler403='django_rest_structure.api.views.error_view_403'handler400='django_rest_structure.api.views.error_view_400'4. Create your result messages like:fromdjango_rest_structure.results.codesimportResultMessageStructurefromdjango_rest_structure.results.exceptionimportResultMessagesclassMyCustomResultMessages(ResultMessages):CUSTOM_ERROR=ResultMessageStructure(1,'My Custom Error',False,500)# `1` is your result code# `My Custom Error` is your result message# `False` define result is success or not# `500` is http response code5. Create your custom exception like:fromdjango_rest_structure.results.exceptionimportErrclassMyCustomError(MyCustomResultMessages,Err):def__init__(self,*args,**kwargs):super(MyCustomError,self).__init__(*args,**kwargs)6. Now you should create your views like:fromdjango_rest_structure.api.viewsimportBaseApiViewclassMyView(BaseApiView):defpost_method(self,request,*args,**kwargs):result={}returnResponseStructure(ResultMessages.GET_SUCCESSFULLY,result)sample 1:request data:{}response data:{"status":{"code":200,"message":"Done","is_success":true},"result":{}}7. Create your serializers like:1. simple serializersfromdjango_rest_structure.api.viewsimportBaseApiViewfromdjango_rest_structure.api.serializersimportBaseSerializerfromrest_frameworkimportserializersclassMySerializer(BaseSerializer):my_field=serializers.CharField()defget_response(self):# the request available on self.requestresponse_data={'my_field':self.validated_data['my_field']}returnResponseStructure(ResultMessages.GET_SUCCESSFULLY,response_data)classMyView(BaseApiView):defpost_method(self,request,*args,**kwargs):returnMySerializer(request.data,check_is_valid=True,request=request).get_response()sample 1:request data:{"my_field":"test"}response data:{"status":{"code":200,"message":"Done","is_success":true},"result":{"my_field":"test"}}2. serializer with custom validationfromdjango_rest_structure.api.viewsimportBaseApiViewfromdjango_rest_structure.api.serializersimportBaseSerializerfromdjango_rest_structure.api.validationsimportStrFieldValidationfromrest_frameworkimportserializersdefmy_field_vld(value,**extra_params):returnvalue.upper()classMySerializer(BaseSerializer):# returned object from StrFieldValidation is available on validated_datamy_field=serializers.CharField(validators=[StrFieldValidation(regex=r'^[A-Za-z]+$',error_message='just enter alphabet',extra_function=my_field_vld)])defget_response(self):# the request available on self.requestresponse_data={'my_field':self.validated_data['my_field']}returnResponseStructure(ResultMessages.GET_SUCCESSFULLY,response_data)classMyView(BaseApiView):defpost_method(self,request,*args,**kwargs):returnMySerializer(request.data,check_is_valid=True,request=request).get_response()sample 1:request data:{"my_field":"@"}response data:{"status":{"code":406,"message":"Entered Data Is Not Valid","is_success":false},"result":{"my_field":["enter just alphabet"]}}sample 2:request data:{"my_field":"Test"}response data:{"status":{"code":200,"message":"Done","is_success":true},"result":{"my_field":"TEST"}}3. list serializerfromdjango_rest_structure.api.viewsimportBaseApiViewfromdjango_rest_structure.api.serializersimportMyListSerializerclassMySerializer(MyListSerializer):defget_response(self):# the request available on self.requestmy_list=['a','b','c','d','e','f','g','h','i','j','k','l']response_data=self.paging_structure(*self.paging(my_list))returnResponseStructure(ResultMessages.GET_SUCCESSFULLY,response_data)classMyView(BaseApiView):defpost_method(self,request,*args,**kwargs):returnMySerializer(request.data,check_is_valid=True,request=request).get_response()sample 1:request data:{"page":1,"count":5}response data:{"status":{"code":200,"message":"Done","is_success":true},"result":{"list":["a","b","c","d","e"],"page":1,"count_in_page":5,"total_count":12}}8. Config your settingschange structure response function by adding to yoursettings.pyREST_STRUCTURE_CONF={'response_handler':'django_rest_structure.results.structure.response_structure'}define your custom function like:defresponse_structure(response):return{'status':{'code':response.message.code,'message':response.message.message,'is_success':response.message.is_success_result,},'result':response.body}change logging handler function by adding to yoursettings.pyREST_STRUCTURE_CONF={'log_handler':'django_rest_structure.logs.console.emmit',}define your custom function like:importlogginglogger=logging.Logger('console')defemmit(request,response,error,request_time,response_time):iferrorisnotNone:logger.debug(error)9. You can see the simple project that use from this package at:django_rest_structure_sample