Skip to content

Django Rest Framework - Tips and Checklists

Tips

  • the best way to clear data is by sending an empty list or value, if the field allows writing this will be handled by the serializer
  • when setting variables consider the possible size of the variable being stored, large objects being set to variables can cause issues with memory
  • use JSONField over DictField
  • delete endpoints should return a 200 or no content

Schemas and Documentation

  • Schema: is a machine readable document that outlines all available API endpoints, URLs and HTTP verbs (post, put, etc)
    • schemas default to the OpenAPI specs
      • drf-spectactular is the recommended third party package to generate OpenAPI 3 schema for DRF
        • can make use of SpectacularAPIView to create a URL for viewing the schema as a part of the API
  • Documentation: added to a schema to make it easier for humans to read and consume
    • two popular options are Redoc and SwaggerUI
      • SwaggerUI is as straightforward as drf-spectacular

Deployment Checklist

  • add environment variables via environs[django]
  • set DEBUG to False
  • set ALLOWED_HOSTS
  • use environment variable for SECRET_KEY
  • update DATABASES to use SQLite locally and PostgreSQL in production
  • configure static files and install whitenoise
  • install gunicorn for a production web server
  • create a requirements.txt file
  • create a Procfile for Heroku
  • create a runtime.txt to set the Python version on Heroku