Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Django rest framework tips and tricks

A few things about Django REST Framework.

  • Identifiez-vous pour voir les commentaires

Django rest framework tips and tricks

  1. 1. Django Rest Framework Tips and Tricks Xavier ORDOQUY @linovia_net
  2. 2. Freelance! (since 2004) Python Django! (Backend) Raven maintainer! (Sentry client) Former! PyGTK! contributor Various! pull requests irc: Linovia @linovia_net Expertise Dev
  3. 3. Reminder • It is Django ! ! • It is Python !
  4. 4. Fully featured • Serializers • ModelSerializers • Parsers • Renderers • Authentication • Permission • Throttling • Routers • Views • ViewSets • Filtering • Testing • Pagination
  5. 5. from rest_framework.decorators import api_view!  ! @api_view(['GET'])! def hello_world(request):! return Response({"message": "Hello, world!"}) but loosely coupled Authentication Content negotiation Serializers Generic views
  6. 6. Customizable web interface
  7. 7. Debug toolbar
  8. 8. pre/post save class MyCreateView(CreateAPIView):! model = models.MyModel! serializer_class = serializers.MySerializer! ! def post_save(self, obj, created):! if created:! obj.reviewers = [user1, user2]!
  9. 9. Class based views class MyMixin(object):! model = models.MyModel! serializer_class = serializers.MySerializer! ! ! class MyCreateView(MyMixin, CreateAPIView):! pass! • MRO: mixins are on the left side
  10. 10. # views.py! class UserViewSet(viewsets.ModelViewSet):! queryset = User.objects.all()! serializer_class = UserSerializer! ! # urls.py! router = DefaultRouter()! router.register(r'users', views.UserViewSet)! ! urlpatterns = patterns('',! url(r'^', include(router.urls)),! )! Viewsets & routers Nice to get started but optional
  11. 11. Auth / Permissions • Auth are for knowing who you are ! • Permissions are to grant you access
  12. 12. Testing Utilities • APIRequestFactory + format ! • force_authenticate • APIClient response = self.client.get('/users/4/')! self.assertEqual(response.data,! {'id': 4, 'username': 'lauren'})!
  13. 13. Performances • Fast to prototype • Easy to tune • Django performance tips also applies !
  14. 14. Tempsemms 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 Full stack Serialisation Redis Content nego Middleware HttpResponse Database lookup Redis lookup Serialization Django request/response API view Response rendering
  15. 15. • Django Rest Framwork Optimization:
 http://dabapps.com/blog/api-performance-profiling-django-rest-framework/

×