5. What’s DRF?
Django REST framework is a powerful and
flexible toolkit for building Web APIs.
5
6. What’s DRF?
u Some reasons you might want to use REST framework:
u The Web browsable API is a huge usability win for your developers.
u Authentication policies including packages
for OAuth1a and OAuth2.
u Serialization that supports both ORM and non-ORM data sources.
u Customizable all the way down - just use regular function-based
views if you don't need the more powerful features.
u Extensive documentation, and great community support.
u Used and trusted by large companies such
as Mozilla and Eventbrite.
6
12. DRF - Serialization
u Creating a Serializer class
u Working with Serializers
u serializers.Serializer
u Using ModelSerializers
Serializers ~ Django’s Forms
12
13. DRF - Requests and Responses
u Request objects:
u REST framework introduces a Request object that extends
the regular HttpRequest, and provides more flexible request
parsing. The core functionality of the Request object is
the request.data attribute, which is similar to request.POST,
but more useful for working with Web APIs.
u Response objects:
u REST framework also introduces a Response object, which is
a type of TemplateResponse that takes unrendered content
and uses content negotiation to determine the correct
content type to return to the client.
u Status codes:
u Using numeric HTTP status codes in your views doesn't always
make for obvious reading, and it's easy to not notice if you
get an error code wrong. REST framework provides more
explicit identifiers for each status code, such
as HTTP_400_BAD_REQUEST in the status module. It's a good
idea to use these throughout rather than using numeric
identifiers.
13
14. DRF - Requests and Responses
u Wrapping API views
u REST framework provides two wrappers you can use to
write API views:
u The @api_view decorator for working with function based
views.
u The APIView class for working with class based views.
14
15. DRF - Class Based Views
u We can also write our API views using class based
views, rather than function based views. As we'll see
this is a powerful pattern that allows us to reuse
common functionality, and helps us keep our
code DRY (don't repeat yourself).
u Writing our API using class based views
u Using mixins
u Using generic class based views
15
16. DRF - Relationships & Hyperlinked APIs
u At the moment relationships within our API are
represented by using primary keys. In this part of the
tutorial we'll improve the cohesion and discoverability of
our API, by instead using hyperlinking for relationships.
u Using primary keys.
u Using hyperlinking between entities.
u Using a unique identifying slug field on the related entity.
u Using the default string representation of the related entity.
u Nesting the related entity inside the parent representation.
u Some other custom representation.
16
17. DRF - ViewSets & Routers
u REST framework includes an abstraction for dealing
with ViewSets, that allows the developer to
concentrate on modeling the state and interactions
of the API, and leave the URL construction to be
handled automatically, based on common
conventions.
u ViewSet classes are almost the same thing
as View classes, except that they provide operations
such as read, or update, and not method handlers
such as get or put.
u Using Routers
u Binding ViewSets
17
18. DRF - Authentication & Permissions
u Authenticating with the API
u Adding required permissions to views
u Custom Permissions
18