Package: django-utilitas
Django UtilitasDjango Utilitas is a library that contains many ready-to-use utility classes so that you can just focus on the business logic of your API.Utilitas itself is built upon many other great Django packages such as drf-ff, drf-yasg and the Django REST Framework itself.Feature summaryHere is a summarized version of Utilitas' features:paginationsortingfiltering (searching)swagger API supportnested read serializerslimiting the response' fieldbulk creation of modelsInstallationpipinstalldjango-utilitasDocumentationDocs are still in progress. I have assignments to do :(Setting up a simple CRUD endpointUtilitas providesBaseListView,BaseDetailsandBaseSearchViewwhich are a more sophisticated version of DRF's default views.
With them, you can set up a powerful API with about 9 lines of codes.# views.pyclassAccountListView(BaseListView):name="Account list view"# optionalmodel=Account# subclass of BaseModelserializer=AccountSerializer# subclass of BaseModelSerializerclassAccountDetailsView(BaseDetailsView):name="Account details view"model=Accountserializer=AccountSerializerclassAccountSearchView(BaseSearchView):name="Account search view"model=Accountserializer=AccountSerializerThen, setup the urls like this. Please note that the argument must be namedobj_id.#urls.pyurlpatterns=[path("accounts/",views.AccountListView.as_view(),name="account-list"),path("accounts/<int:obj_id>",views.AccountDetailsView.as_view(),name="account-details"),path("accounts/search",views.AccountSearchView.as_view(),name="account-search"),]Then, when you go to your swagger endpoint, you should be able to see something like this:Bulk CreationWhen a POST request is made to list endpoints with thebulkquery parameter set to a truthy value, aListSerializerwill be used to perform the creation process.Request exampleimportrequestsrequests.post("api/books/?bulk=True",{# need to set this 'objects' parameter in the request body"objects":[{"title":"The Hobbit","author":"J. R. R. Tolkein"},{"title":"History of Burma","author":"G. E. Harvey"}]})ImplementationYou can either inherit the Meta class from theBaseModelSerializerclassclassBookSerializer(BaseModelSerializer):classMeta(BaseModelSerializer.Meta):model=Bookfields="__all__"Or, set thelist_serializer_classoption toutilitas.serializers.BaseListSerializer. (more information here:https://www.django-rest-framework.org/api-guide/serializers/#listserializer)fromutilitas.serializersimportBaseListSerializerclassBookSerializer(BaseModelSerializer):classMeta:model=Bookfields="__all__"list_serializer_class=BaseListSerializerFor more information about django-utilitas, please read the architecture documenthereGetting a CSV responseClient just need to set a query parameter namedcsvto true. This works in BaseListView and BaseSearchView instances.Request exampleimportrequestsrequests.get("api/books?csv=true")Changelog1.3.14removed field validatoin for filter_param1.3.13bug fixes1.3.12added field validation in search endpoint's filter_param1.3.11made the '_translate_expand_params' function public1.3.10bulk-creation bug fixed. Dumb me didn't change anything in the views.py file D:1.3.9bug fixes1.3.8bug fixes1.3.7bug fixes1.3.6added an alias option for model fields. (user_friendly_field attribute)1.3.5bug fixes1.3.4bug fixes1.3.3forgot to add csv library. hehe.1.3.2added a csv-returning functionality.1.3.1now, the API won't return 500 if missing foreign keys are provided in theexpandparameter.1.3.0removed the need forrelated_fieldsparameter. Prefetching related fields is now done automatically using theexpandparameter provided by the client.1.2.9renamed file frommigrations.pytomiddlewares.py(my stupid mistake in the first place)1.2.8bug fixes1.2.7bug fixes1.2.6bug fixes1.2.5addedexclude_paramsoption in search endpoints.1.2.4fixed bug in pagination1.2.3lowered required Python version1.2.2increased character limit forFilterParamSerializer's fields.1.2.1added a middleware for logging database queries in each requestbug fixes1.2.0added bulk creation1.1.4bug fixes1.1.3bug fixes1.1.2 (the first usable version)bug fixes