djangosnippets.org: Latest snippets tagged with 'django-rest-framework'http://djangosnippets.org/tags/django-rest-framework/2019-12-11T08:02:03.303049-06:00Django Standard API Response Middleware for DRF
2019-12-11T08:02:03.303049-06:00agusmakmunhttp://djangosnippets.org/snippets/10717/<p>As you can see, if you using django-rest-framework, you will found many different response format. This middleware to solve all of these problems with Standard API Response.</p>
<p>All HTTP Response status stored into json response, not in HTTP Status (because mobile application, like android can't fetch the response body when …</p>
Freely redistributableURLMan serializer field for Django Rest Framework (DRF)
2019-08-13T04:40:10.121866-05:00rixxhttp://djangosnippets.org/snippets/10677/<p>This snippet shows how to add a <code>url</code> field to your API objects, which will then show up as an object in your JSON output.</p>
<p>As parameters, you can specify:</p>
<ul>
<li><code>urls</code>: A list of strings that exist on your URLMan class</li>
<li><code>attribute</code>: The name of the URLMan class …</li></ul>
Freely redistributableDifferent Response Data/Serializer for DRF Views/Viewsets
2018-12-12T09:19:21.107136-06:00shivanshs9http://djangosnippets.org/snippets/10656/<p>Mixin for Django Rest Framework View/Viewsets to use different response data/serializers without unnecessary hacks.</p>
<p>Just extend your DRFs view/viewset with this mixin after the DRF's APIView (sub)classes.</p>
Freely redistributableDRF - Optimizing ModelViewSet queries
2018-05-18T11:42:14.426508-05:00jackton1http://djangosnippets.org/snippets/10647/<p>Using Django REST Framework for Model views there is always the issue of making duplicated queries without either prefetching the objects that will be accessed using the serializer and as such will lead to large number of queries being made to the database.</p>
<p>This will help in optimizing the queryset …</p>
Freely redistributableCustom DRF browsable API interface for InBBoxFilter of django-rest-framework-gis
2017-03-02T10:23:58.514032-06:00iamtiohttp://djangosnippets.org/snippets/10606/<p><a href="http://www.django-rest-framework.org/api-guide/filtering/#customizing-the-interface">DRF browsable API interface</a> for django-rest-framework-gis <a href="https://github.com/djangonauts/django-rest-framework-gis#inbboxfilter">InBBoxFilter</a></p>
<p>Tested with</p>
<pre><code>Django==1.10.5
django-filter==1.0.1
djangorestframework==3.5.4
djangorestframework-gis==0.11
</code></pre>
<p><img alt="Screenshot" src="http://joxi.ru/8AnzxLDTOEgeAO.png" /></p>
Freely redistributableDjango Rest Framework LoginExemptPermission: Apply `IsAuthenticated` to all ViewSet actions except those specified as exempt of authentication
2016-10-18T10:07:38.085247-05:00AgustinLadohttp://djangosnippets.org/snippets/10592/<p>Say you want to keep your API secure and thus it has authentication, but there's this one View action in a ViewSet which unlike the rest of the ViewSet's actions needs to allow free access without authentication.</p>
<p>This solution applies the good old <code>IsAuthenticated</code> permission to all ViewSet actions except …</p>
Freely redistributableJSON Web Token authentication middleware
2016-04-13T14:03:15.233277-05:00andruwharthttp://djangosnippets.org/snippets/10562/<p>This hasn't been thoroughly tested yet but so far it works great. We had no use for sessions or the built in authentication middleware for django as this was built to be a microservice for authentication. Unfortunately if you just use the django rest framework-jwt package the authentication occurs at …</p>
Freely redistributable