rest_framework框架简介 官方网站:https://www.django-rest-framework.org/
安装
pip install djangorestframework
settings.py文件配置
INSTALLED_APPS = [
'rest_framework'
]
在app中创建一个serializers.py文件
serializers.py内容编写
from rest_framework import serializers
from .models import Student
class StuSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = '__all__'
views.py文件内容
from rest_framework import viewsets
from .serializer import StuSerializer
class StusViewSet(viewsets.ModelViewSet):
queryset = Student.objects.filter(zy_id="4")
serializer_class = StuSerializer
urls.py 文件内容:
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)),
]
运行调试