Package: drf-generators
DRF GeneratorsWriting APIs can be boring and repetitive work. Don’t write another CRUDdy view inDjango Rest Framework. With DRF Generators, one simple command will generate all of your Views, Serializers, and even Urls for your Django Rest Framework application!For a full step-by-step tutorial, check out myblog post!This isnotintended to give you a production quality API. It was intended to jumpstart your development and save you from writing the same code over and over for each model.InstallationUsageSerializersViewsUrlsTestsLicenseInstallationInstall with pip:$pipinstalldrf-generatorsor Clone the repo and install manually:$gitclonehttps://github.com/brobin/drf-generators.git$cddrf-generators$pythonsetup.pyinstallTo use DRF Generators, add it your INSTALLED_APPS.INSTALLED_APPS=(...'rest_framework','drf_generators',...)Note: In order to use the APIView classes, you must have the rest framework DEFAULT_PAGINATION_CLASS and PAGE_SIZE set.REST_FRAMEWORK={'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination','PAGE_SIZE':15}UsageTo use the generators, run the following command, whereappis the application to generate an API for.$pythonmanage.pygenerate{app}{options}OptionAction--serializersGenerate only Serializers for your app.--viewsGenerate only Views for your app.--urlsGenerate only urls for your app.--forceOverwrite existing files without the warning prompt.-f,--formatFormat to use when generating views and urls. Valid options:viewset,apiview,function,modelviewset. Default:viewset.-d,--depthSerialization depth for related models. Default: 0Example:Generate everything for the appapiwith function style views, overwriting existing files, with a serialization depth of 2.$pythonmanage.pygenerateapi--formatfunction--force--depth=2SerializersDrf Generators will createserializers.pyfor your application. It currently uses rest framework’sModelSerializerfor serialization of the models defined inmodels.py.classModelSerializer(serializers.ModelSerializer):classMeta:model=UserViewsDRF Generators will createviews.pyfor your application. It can generateViewSet,APIViewand function based views. Set the--formatoption when running the generator to pick the preferred styleViewSetpython manage.py generate api--formatviewsetclassModelViewSet(ViewSet):deflist(self,request):...defcreate(self,request):...defretrieve(self,request,pk=None):...defupdate(self,request,pk=None):...defdestroy(self,request,pk=None):...APIViewpython manage.py generate api--formatapiviewclassModelAPIView(APIView):defget(self,request,id,format=None):...defput(self,request,id,format=None):...defdelete(self,request,id,format=None):...classModelAPIListView(APIView):defget(self,request,format=None):...defpost(self,request,format=None):...Functionpython manage.py generate api--formatfunction@api_view(['GET','POST'])defmodel_list(request):ifrequest.method=='GET':...elifrequest.method=='POST':...@api_view(['GET','PUT','DELETE'])defmodel_detail(request,pk):ifrequest.method=='GET':...elifrequest.method=='PUT':...elifrequest.method=='DELETE':...ModelViewSetpython manage.py generate api--formatmodelviewsetclassMyModelViewSet(ModelViewSet):queryset=MyModel.objects.all()serializer_class=MyModelSerializerUrlsFinally, DRF Generator will create you a defaulturls.pyto match the View format you are using.ViewSet & ModeViewSet Routesrouter=SimpleRouter()router.register(r'model',views.ModelViewSet,'Model')urlpatterns=router.urlsAPIView urlsurl(r'^model/([0-9]+)$',views.ModelAPIView.as_view()),url(r'^model',views.ModelAPIListView.as_view()),Function urlsurlpatterns=[url(r'^model/(?P<pk>[0-9]+)$',views.model_detail),url(r'^model/$',views.model_list),]urlpatterns=format_suffix_patterns(urlpatterns)TestsA full application built with drf-generators can be found in thetests directory. Instructions on running the tests can be found in the test project’s README.LicenseMIT License. SeeLICENSE.