1、models.py
2、同目录创建serializers.py
from rest_framework.serializers import ModelSerializer from .models import File
3、修改views.py
from rest_framework import generics from .models import File from .serializers import FileSerializer class FileList(generics.ListCreateAPIView): """ 列出所有的files或者创建一个新的file。 """ queryset = File.objects.all() serializer_class = FileSerializer class FileViewSet(generics.RetrieveUpdateDestroyAPIView): """ 文件操作 允检索,更新或删除一个file示例。 """ queryset = File.objects.all() serializer_class = FileSerializer
4、修改同目录urls.py
from rest_framework.urlpatterns import format_suffix_patterns from . import views from django.urls import path urlpatterns = [ path('file/', views.FileList.as_view()), path('file/<int:pk>/', views.FileViewSet.as_view()), ] urlpatterns = format_suffix_patterns(urlpatterns)
5、运行并访问