Django rest_framework 增删改查
mixins.ListModelMixin 查询全部信息 GET请求
mixins.RetrieveModelMixin 查询部分信息 GET请求
mixins.UpdateModelMixin为更新 put 要提前提交全部信息 PUTCH 更新部分信息
mixins.DestroyModelMixin 删除 DELETE 请求
mixins.CreateModelMixin 增加 POST请求
添加、获取全部数据
views:
# 添加、全部获取数据 class UserAPIView(ModelViewSet): queryset = User.objects.all()
serializer_class = UserSerializer
urls:
from rest_framework.routers import DefaultRouter
from app02.views import *
rount = DefaultRouter()
urlpatterns = []
rount.register(r'^user', views.UserAPIView)
urlpatterns += rount.urls
修改 、删除
views:
from rest_framework import generics
from .models import User # 导入数据库
from .serializers import UserSerializer # 导入当前目录配置好的序列化器
class WorkFlowLineUpdate(generics.UpdateAPIView,mixins.DestroyModelMixin):
"""
url获取pk,修改时指定序列化类和query_set
"""
model = User
serializer_class = UserSerializer
queryset = model.objects.all()
urls:
from django.conf.urls import url,
urlpatterns = [
url(r'^updateWorkFlowLine/(?P<pk>\d+)/$', WorkFlowLineUpdate.as_view()),
]