Rest_framework Overview Official website: www.django-rest-framework.org/

The installation

pip install djangorestframework
Copy the code

Settings. py file configuration

INSTALLED_APPS = [
    'rest_framework'
]
Copy the code

Create serializers. Py in your app

Serializers. Py content writing

from rest_framework import serializers
from .models import Student

class StuSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = '__all__'
Copy the code

Views. py File contents

from rest_framework import viewsets
from .serializer import StuSerializer

class StusViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.filter(zy_id="4")
    serializer_class = StuSerializer
Copy the code

Content of urls.py file:

from django.conf.urls import url
from django.urls import include
from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('stus', views.StusViewSet)

urlpatterns = [
    url('', include(router.urls)),
]
Copy the code

Run the debug