Django REST framework basics

Introduction

The Django REST framework makes it easy to create APIs. Here's what you need to know about the Django REST framework.

Addition to settings.py

Add rest_framework to ʻINSTALLED_APPS`.

settings.py


INSTALLED_APPS = [
    'rest_framework', #add to
]

Create serializer

Next, we will create a serializer.

serializers.py


from rest_framework import serializers


class SampleSerializer(serializers.ModelSerializer):

    class Meta:
        model =Object model
        fields =Fields to include (in all cases'__all__') #Or exclude= (Fields you want to exclude)

Create view

After creating the serializer, add it to views.py.

views.py


from rest_framework import generics


class SampleListAPI(generics.ListAPIView):
    queryset =Object model.objects.all()
    serializer_class =Serializer


class SampleDetailAPI(generics.ListAPIView):
    queryset =Object model.objects.all()
    serializer_class =Serializer

ʻUrls.py` settings

Finally, set the routing and you're done.

urls.py


from django.urls import path

from . import views


urlpatterns = [
    path('api/sample/list', views.SampleListAPI.as_view(), name='api_sample_list'),
    path('api/sample/detail/<int:pk>/', views.SampleDetailAPI.as_view(), name='api_sample_detail'),
]

Summary

Here, I explained how to create an API using the Django REST framework.

Recommended Posts

Django REST framework basics
Django Rest Framework Tips
Django basics
Django REST framework stumbling block
Django REST framework with Vue.js
Login with django rest framework
[Django] Use MessagePack with Django REST framework
Create RESTful APIs with Django Rest Framework
Logical deletion in Django, DRF (Django REST Framework)
Understand the benefits of the Django Rest Framework
ng-admin + Django REST framework ready-to-create administration tool
CRUD GET with Nuxt & Django REST Framework ②
Miscellaneous notes about the Django REST framework
CRUD POST with Nuxt & Django REST Framework
CRUD GET with Nuxt & Django REST Framework ①
Django REST Framework + Clean Architecture Design Consideration
Django python web framework
CRUD PUT, DELETE with Nuxt & Django REST Framework
Django REST framework A little useful to know.
Implement JWT login functionality in Django REST framework
Implementing authentication in Django REST Framework with djoser
Create a Todo app with Django REST Framework + Angular
More new user authentication methods with Django REST Framework
Create a Todo app with the Django REST framework
Create APIs around user authentication with Django REST Framework
List method for nested resources in Django REST framework
Implement APIs at explosive speed using Django REST Framework
[Django Rest Framework] Customize the filter function using Django-Filter
Implement hierarchical URLs with drf-nested-routers in Django REST framework
How to reset password via API using Django rest framework
Django rest framework decorators ʻaction decorator replaces list_route and detail_route`
Implementation of JWT authentication functionality in Django REST Framework using djoser
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
Django
Install Python framework django using pip
Create a REST API to operate dynamodb with the Django REST Framework
Implementation of custom user model authentication in Django REST Framework with djoser
How to deal with garbled characters in json of Django REST Framework
I made a webAPI! Build environment from Django Rest Framework 1 on EC2
How to get people to try out django rest framework features in one file
How to automatically generate API document with Django REST framework & POST from document screen