Appearance
Django Rest Framework - Basics
REST
Definition: REpresentation State Transfer
- is stateless like HTTP
- supports common HTTP verbs
- returns data in either JSON or XML
Views
Are used to customize and serialize data in JSON format.
- views rely on the model, URL and serializer
- allow us to apply logic to each API endpoint
Viewsets
Are used to replace multiple related views into a single class.
- instead of a list view and a detail view, a viewset can handle both list and detail views
- use permission_classes the same as views
- define the view behanvior
Serializers
Translates complex data like querysets and model instances into a format that is easy to consume over the internet. Also deserializes data which is the same process in reverse, JSON data is validated and then transformed into a dictionary.
- REST Framework does not attempt to optimize querysets so serializers usually need to be tweaked for performance reasons
- define the API representation
Including Data
Data is made to be "included" by adding the field to the "included_serializers" section. When it is in this section it can be included in response data by adding it to the the "include" query param. This data will show up in the "included" section of the response data. If this data is a relationship, it will also show up in the relationships section of the response data.
URLs
- always make sure to version urls (
/v1) so that if there is a major refactor you can migrate over to/v2
Router
Are used with viewsets to automatically generated URLs.
- two default routers:
SimpleRouterandDefaultRouter
