django rest framework tips and tricks

16
Django Rest Framework Tips and Tricks Xavier ORDOQUY @linovia_net

Upload: xordoquy

Post on 15-Jul-2015

319 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Django rest framework   tips and tricks

Django Rest Framework Tips and Tricks

Xavier ORDOQUY @linovia_net

Page 2: Django rest framework   tips and tricks

Freelance!(since 2004) Python

Django!(Backend)

Raven maintainer!(Sentry client)

Former!PyGTK!

contributor

Various!pull requests

irc: Linovia@linovia_net

Expertise

Dev

Page 3: Django rest framework   tips and tricks

Reminder

• It is Django !

!

• It is Python !

Page 4: Django rest framework   tips and tricks

Fully featured• Serializers

• ModelSerializers

• Parsers

• Renderers

• Authentication

• Permission

• Throttling

• Routers

• Views

• ViewSets

• Filtering

• Testing

• Pagination

Page 5: Django rest framework   tips and tricks

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

Page 6: Django rest framework   tips and tricks

Customizable web interface

Page 7: Django rest framework   tips and tricks

Debug toolbar

Page 8: Django rest framework   tips and tricks

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]!

Page 9: Django rest framework   tips and tricks

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

Page 10: Django rest framework   tips and tricks

# 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

Page 11: Django rest framework   tips and tricks

Auth / Permissions

• Auth are for knowing who you are

!

• Permissions are to grant you access

Page 12: Django rest framework   tips and tricks

Testing Utilities

• APIRequestFactory + format !

• force_authenticate

• APIClient

response = self.client.get('/users/4/')!self.assertEqual(response.data,! {'id': 4, 'username': 'lauren'})!

Page 13: Django rest framework   tips and tricks

Performances

• Fast to prototype

• Easy to tune

• Django performance tips also applies !

Page 14: Django rest framework   tips and tricks

Tem

ps e

m m

s

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 SerializationDjango request/response API view Response rendering

Page 15: Django rest framework   tips and tricks
Page 16: Django rest framework   tips and tricks

• Django Rest Framwork Optimization: http://dabapps.com/blog/api-performance-profiling-django-rest-framework/