Django REST Framework
Installation
Getting started with DRF is straightforward.
Prerequisites
- Python (3.6+)
- Django (3.0+)
Step-by-step Guide
- Install using pip:
pip install djangorestframework - Add to
INSTALLED_APPS: Open your project'ssettings.pyand add'rest_framework'to your installed apps.INSTALLED_APPS = [ ... 'rest_framework', ] - Global Settings (Optional): You can configure global DRF settings in
settings.pyusing theREST_FRAMEWORKdictionary.REST_FRAMEWORK = { # Use Django's standard `django.contrib.auth` permissions, # or allow read-only access for unauthenticated users. 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' ] }
Optional Packages
None of these are required, but each adds a capability you are likely to want as an API grows:
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support.
pip install drf-spectacular # Schema generation support.
pip install djangorestframework-simplejwt # JSON Web Token authentication plugin for DRF.
pip install drf-writable-nested # Nested create and update support for serializers.
Introduction
Django REST Framework (DRF) is a toolkit for building Web APIs on top of Django. It does not replace Django - it layers onto it, reusing the ORM, settings, URL routing and authentication system you already know and supplying the pieces Django leaves out when a response is JSON instead of HTML.
Serializers
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types.
