Skillversitas

Building an API with DRF

The DRF is the API the no the code: the Serializer (the model the JSON, the validate, the no the SQL), the ViewSet (the 5 the route, the router, the no the view), and the permission (the IsAuthenticated, the 403 the no the 1). The token is the no the session (the Authorization the header, the no the cookie), the throttle is the no the 1 (the 100 the 1 the hour), and the pagination is the 20 the 1 (the no the 10K). This chapter covers the Serializer (the no the model, the validate), the ViewSet (the 5, the router), the token (the header, the no the cookie), and the throttle (the no the 1).

The Serializer and the ViewSet

# the SERIALIZER (the model the
#   JSON, the no SQL, the
#   validate, the no the 1):
from rest_framework import serializers
from .models import Note

class NoteSerializer(serializers.ModelSerializer):
    #   the no: the 2 (the model,
    #     the fields, the no
    #     the 1):
    class Meta:
        model = Note
        fields = ["id", "title",
            "body", "created"]
        #   the no: the 1 (the
        #     the no, the no
        #     the no, the no
        #     the no):
    #   the read (the 1, the no,
    #     the no the 1):
    #   def get_title(self, obj):
    #       return obj.title.strip()
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)

# the VIEWSET (the 5, the router,
#   the no view, the no the 1):
from rest_framework import (
    viewsets, routers,
    permissions)

class NoteViewSet(viewsets.ModelViewSet):
    queryset = Note.objects.all()
    serializer_class = NoteSerializer
    #   the 5: the no (the list,
    #     the create, the
    #     the retrieve, the
    #     the update, the
    #     the destroy, the
    #     the no the 1):
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    permission_classes = [
        permissions.IsAuthenticated,
    ]
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)

# the ROUTER (the 1, the no, the
#   no view, the no the 1):
#   the api/urls.py:
from rest_framework import routers
from .views import NoteViewSet

router = routers.DefaultRouter()
router.register("notes", NoteViewSet,
    basename="note")
urlpatterns = router.urls
#   the no: the 5 (the /api/notes/
#     the GET, the POST, the
#     /api/notes/1/ the GET,
#     the PUT, the DELETE, the
#     the no the 1)

The Serializer is the model the JSON: the ModelSerializer is the no the 1 (the Meta.model plus the fields, the no the 1), the validate is the 1 (the clean, the no the 1, the no the 1), and the no is the SQL (the ORM the no, the no the the) - the get_ is the read (the 1, the no the 1, the no the the), and the no is the 1 (the no the 1, the no the the). The ViewSet is the 5: the list (the GET the 200), the create (the POST the 201), the retrieve (the GET the 1), the update (the PUT the 200), and the destroy (the DELETE the 204) - the router is the 1 (the register, the no the view, the basename the no the 1), and the permission is the 403 (the IsAuthenticated, the no the 1, the no the 401) - the token is the next: the header the no the cookie.

The token, the throttle, and the pagination

# the TOKEN (the header, the no
#   cookie, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
pip install djangorestframework-simplejwt
#   the settings (the no, the
#     the 1, the no
#     the no):
INSTALLED_APPS += [
    "rest_framework",
    "rest_framework_simplejwt",
]
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "rest_framework_simplejwt."
        "authentication."
        "JWTAuthentication",
    ),
    "DEFAULT_PERMISSION_CLASSES": (
        "rest_framework.permissions."
        "IsAuthenticated",
    ),
}
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the 1: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
curl -X POST http://api.local/api/token/ \
    -d "username=u1&password=x"
#   the no: the access +
#     the refresh (the 2, the
#     the no, the no
#     the 1):
#   the 2: the no (the no,
#     the no, the no
#     the no, the no
#     the no):
curl http://api.local/api/notes/ \
    -H "Authorization: Bearer
    eyJ0eXAiOi..."
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the THROTTLE (the no 1, the
#   100 the hour, the no the
#   no):
#   the REST_FRAMEWORK:
#   the "DEFAULT_THROTTLE_CLASSES":
#     (
#         "rest_framework."
#         "throttling."
#         "AnonRateThrottle",
#         "rest_framework."
#         "throttling."
#         "UserRateThrottle",
#     ),
#   the "DEFAULT_THROTTLE_RATES": {
#       "anon": "10/hour",
#       "user": "100/hour",
#   }
#   the no: the no (the 429,
#     the no, the no
#     the no, the no
#     the no)

# the PAGINATION (the 20, the no
#   10K, the no the 1):
#   the REST_FRAMEWORK:
#   the "DEFAULT_PAGINATION_CLASS":
#     "rest_framework.pagination."
#     "PageNumberPagination",
#   the "PAGE_SIZE": 20
#   the no: the 1 (the 20,
#     the no, the no
#     the no, the no
#     the no)

The token is the no the session: the JWT (the simplejwt, the no the 1) is the 2 (the access the 1 the min, the refresh the 1 the day), the Authorization: Bearer is the header (the no the cookie, the CORS the no the 1), and the no is the 1 (the refresh, the no the no, the no the 1) - the 401 is the no (the token the no, the no the 403, the no the 1). The throttle is the no the 1: the AnonRateThrottle is the 10 the 1 the hour (the no the 1), the UserRateThrottle is the 100 the 1 the hour (the 1, the no the 1), and the 429 is the no (the 1, the no the 1, the Retry-After the 1) - the pagination is the 20 the 1: the PageNumberPagination is the 1 (the count, the next, the no the 10K, the no the 1), and the rule is the one: the Serializer the no the model, the ViewSet the 5, the token the header, and the throttle the no the 1 - the next chapter's testing is the client the no the server the no the DB.

The Working Rules

  • The Serializer is the no model, the validate the 1. The ModelSerializer the Meta (the model, the fields, the no the 1), and the clean the 1 (the no the 1, the no the 1) - the no the SQL (the ORM the no), and the get_ the read (the 1, the no the 1).
  • The ViewSet is the 5, the router the 1. The list the POST (the 201), the retrieve the 1 (the 404), and the destroy the 204 (the no the body) - the router.register the no (the basename, the no the 1), and the IsAuthenticated the 403 (the no the 401, the no the 1).
  • The token is the header, the no cookie. The simplejwt the 2 (the access the 1 the min, the refresh the 1 the day), and the Bearer the no (the CORS the no the 1) - the 401 the no (the token, the no the 403), and the no the 1 (the refresh, the no the no).
  • The throttle is the no 1, the 429 the no. The Anon the 10 (the no the 1), and the User the 100 (the 1, the no the 1) - the 429 the 1 (the no the 1, the Retry-After the 1), and the pagination the 20 (the no the 10K, the next the 1).

Summary

The DRF is the API the no code: the Serializer the no the model (the ModelSerializer the Meta, the validate the 1, the no the SQL, the get_ the read), the ViewSet the 5 (the list the 200, the create the 201, the retrieve the 404, the update the 200, the destroy the 204, the router the no the 1), the permission the 403 (the IsAuthenticated, the no the 401, the no the 1), and the token the header (the simplejwt the 2, the Bearer the no the cookie, the 401 the no). The throttle is the no the 1 (the Anon the 10, the User the 100, the 429 the no, the Retry-After), and the pagination is the 20 the 1 (the PageNumberPagination, the no the 10K, the next the 1). The rule is the one: the serializer the no the model, the viewset the 5, the token the header, and the throttle the no the 1 - the next chapter's testing is the client: the no the server, the no the DB, and the 200 the no the 1 the code.