码迷,mamicode.com
首页 > 其他好文 > 详细

Django rest_framework 增删改查

时间:2021-04-01 13:14:48      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pos   目录配置   bsp   delete   route   class   workflow   str   取数   

 

mixins.ListModelMixin 查询全部信息 GET请求
mixins.RetrieveModelMixin 查询部分信息 GET请求
mixins.UpdateModelMixin为更新 put 要提前提交全部信息 PUTCH 更新部分信息
mixins.DestroyModelMixin 删除 DELETE 请求
mixins.CreateModelMixin 增加 POST请求

ModelViewSet

 

 

 

添加、获取全部数据

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()),
]

 

Django rest_framework 增删改查

标签:pos   目录配置   bsp   delete   route   class   workflow   str   取数   

原文地址:https://www.cnblogs.com/ZH-YC/p/14603291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!