博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django rest_framework中的mixins,generics,ModelViewSet中的url简写
阅读量:5082 次
发布时间:2019-06-13

本文共 8821 字,大约阅读时间需要 29 分钟。

models.py

from django.db import modelsclass UserInfo(models.Model):    name = models.CharField(max_length=32)    pwd = models.CharField(max_length=32)class UserToken(models.Model):    token = models.CharField(max_length=128)    userinfo = models.OneToOneField(to="UserInfo")class Publisher(models.Model):    name = models.CharField(max_length=64)    email = models.EmailField()    def __str__(self):        return self.nameclass Book(models.Model):    title=models.CharField(max_length=64)    price = models.IntegerField()    pub_date = models.DateField()    publisher=models.ForeignKey(to='Publisher')    authors = models.ManyToManyField(to="Author")    def __str__(self):        return self.titleclass Author(models.Model):    name = models.CharField(max_length=48)    age = models.IntegerField(default=2)    def __str__(self):        return self.name

seria.py

from app01 import modelsfrom rest_framework import serializersclass PublisherModelSerializer(serializers.ModelSerializer):    class Meta:        model = models.Publisher        fields = "__all__"class BookModelSerializer(serializers.ModelSerializer):    class Meta:        model = models.Book        fields = "__all__"        # 以上是默认的        # 显示超链接    publisher = serializers.HyperlinkedIdentityField(        view_name="publisher_detail",  # url中的别名        lookup_field="publisher_id", #关联字段的字段名        lookup_url_kwarg="pk",    )    # 默认一对多,多对多显示主键,可以自定义显示字段为name和其他字段, 可以不加就显示全部  publisher用自定义的话post请求要重写create方法    # publisher = serializers.CharField(source="publisher.name",read_only=True)#一对多可以用 自定义字段    # authors = serializers.CharField(source="authors.all") #多对多不好用    # 多对多自定义显示字段用下面这个,默认显示主键    # authors =serializers.SerializerMethodField() 自定义字段    # def get_authors(self,obj):    #     temp = []    #     for obj in obj.authors.all():    #         temp.append(obj.name)    #         print(temp)    #     return temp    # 自定义显示字段 用自定义的话post请求要重写create方法,不自定义用默认的就不需要create方法    # def create(self,validated_data):    #     print("validated_data",validated_data)    #     book = models.Book.objects.create(title=validated_data["title"],price=validated_data["price"],pub_date=validated_data["pub_date"],publisher_id=validated_data["publisher"].split("/")[-2])    #     book.authors.add(*validated_data["authors"])    #     return bookclass AuthorModelSerializer(serializers.ModelSerializer):    class Meta:        model = models.Author        fields = "__all__"

seria2.py

from app01 import modelsfrom rest_framework import serializersclass PublisherModelSerializer(serializers.ModelSerializer):    class Meta:        model = models.Publisher        fields = "__all__"class BookModelSerializer(serializers.ModelSerializer):    # 默认一对多,多对多显示主键,可以自定义显示字段为name和其他字段, 可以不加就显示全部  publisher用自定义的话post请求要重写create方法    publisher = serializers.CharField(source="publisher.pk",read_only=True)#一对多可以用 自定义字段不用加read_only=True    # authors = serializers.CharField(source="authors.all") #多对多不好用    # 多对多自定义显示字段用下面这个,默认显示主键    # authors =serializers.SerializerMethodField() #自定义字段    # def get_author(self,obj):    #     temp = []    #     for obj in obj.authors.all():    #         temp.append(obj.name)    #         print(temp)    #     return temp    # 自定义显示字段 用自定义的话post请求要重写create方法,不自定义用默认的就不需要create方法    class Meta:        model = models.Book        fields = "__all__"        read_only_fields = ["id"]    def create(self,validated_data):        print("validated_data",validated_data)        book = models.Book.objects.create(title=validated_data["title"],price=validated_data["price"],pub_date=validated_data["pub_date"],publisher_id=validated_data["publisher"]["pk"])        book.authors.add(*validated_data["authors"])        return bookclass AuthorModelSerializer(serializers.ModelSerializer):    class Meta:        model = models.Author        fields = "__all__"

views.py

from django.shortcuts import renderfrom app01 import modelsfrom app01 import seriafrom rest_framework import mixinsfrom rest_framework import generics#*********第一种继承方法class PublsherMixView(mixins.ListModelMixin,                      mixins.CreateModelMixin,                      generics.GenericAPIView):    queryset = models.Publisher.objects.all()    serializer_class = seria.PublisherModelSerializer    def get(self,request,*args,**kwargs):        return self.list(request,*args,**kwargs)    def post(self,request,*args,**kwargs):        return self.create(request,*args,**kwargs)class PublisherDetailMix(mixins.RetrieveModelMixin,                         mixins.UpdateModelMixin,                         mixins.DestroyModelMixin,                         generics.GenericAPIView):    queryset = models.Publisher.objects.all()    serializer_class = seria.PublisherModelSerializer    def get(self, request, *args, **kwargs):        return self.retrieve(request, *args, **kwargs)    def put(self, request, *args, **kwargs):        return self.update(request, *args, **kwargs)    def delete(self, request, *args, **kwargs):        return self.destroy(request, *args, **kwargs)class BookMixView(mixins.ListModelMixin,                      mixins.CreateModelMixin,                      generics.GenericAPIView):    queryset = models.Book.objects.all()    serializer_class = seria.BookModelSerializer    def get(self,request,*args,**kwargs):        return self.list(request,*args,**kwargs)    def post(self,request,*args,**kwargs):        print("=======")        print(request.data)        return self.create(request,request.data,*args,**kwargs)class BookDetailMix(mixins.RetrieveModelMixin,                         mixins.UpdateModelMixin,                         mixins.DestroyModelMixin,                         generics.GenericAPIView):    queryset = models.Book.objects.all()    serializer_class = seria.BookModelSerializer    def get(self, request, *args, **kwargs):        return self.retrieve(request, *args, **kwargs)    def put(self, request, *args, **kwargs):        print("$$$$$$$")        print(request.data)        return self.update(request, *args, **kwargs)    def delete(self, request, *args, **kwargs):        return self.destroy(request, *args, **kwargs)##***************第二种继承方法from app01 import seria2from rest_framework import mixinsfrom rest_framework import genericsclass PublsherGenView(generics.ListCreateAPIView):    queryset = models.Publisher.objects.all()    serializer_class = seria2.PublisherModelSerializerclass PublisherDetailGen(generics.RetrieveUpdateDestroyAPIView):    queryset = models.Publisher.objects.all()    serializer_class = seria2.PublisherModelSerializerclass BookGenView(generics.ListCreateAPIView):    queryset = models.Book.objects.all()    serializer_class = seria2.BookModelSerializerclass BookDetailGen(generics.RetrieveUpdateDestroyAPIView):    queryset = models.Book.objects.all()    serializer_class = seria2.BookModelSerializer#####*********第三种********viewsets.ModelViewSet**from rest_framework import viewsetsclass BookViewSet(viewsets.ModelViewSet):    queryset = models.Book.objects.all()    serializer_class = seria2.BookModelSerializer

urls.py

from django.conf.urls import url,includefrom django.contrib import adminfrom app01 import views#第三种url简写#简写第一步from rest_framework import routersrouters = routers.DefaultRouter()routers.register(r"books001",views.BookViewSet) #books001为URL前缀urlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^publisher/$', views.PublsherMixView.as_view()),    # url(r'^publisher/(\d+)/$', views.PublisherDetailView.as_view()), #不加超链接    url(r'^publisher/(?P
\d+)/', views.PublisherDetailMix.as_view(), name="publisher_detail"), url(r'^book/$', views.BookMixView.as_view()), url(r'^book/(?P
\d+)/$', views.BookDetailMix.as_view()), ##**********第二种******* url(r'^publishers/$', views.PublsherGenView.as_view()), url(r'^publishers/(?P
\d+)/$', views.PublisherDetailGen.as_view()), url(r'^books/$', views.BookGenView.as_view(),name="book_list"), url(r'^books/(?P
\d+)/$', views.BookDetailGen.as_view(),name="book_detail"), ##*****第三种 这种还可以简写 # url(r'^books1/$', views.BookViewSet.as_view({"get":"list","post":"create"}),name="book_list"), # url(r'^books1/(?P
\d+)/$', views.BookViewSet.as_view({
# 'get': 'retrieve', # 'put': 'update', # 'patch': 'partial_update', # 'delete': 'destroy' # }),name="book_detail"), ##简写第二步 url(r'^', include(routers.urls)),]#简写默认生成如下url# # ^books001/$ [name='book-list']# ^ ^books001\.(?P
[a-z0-9]+)/?$ [name='book-list']# ^ ^books001/(?P
[^/.]+)/$ [name='book-detail']# ^ ^books001/(?P
[^/.]+)\.(?P
[a-z0-9]+)/?$ [name='book-detail']

 项目地址:https://github.com/CH-chen/mixns_demo

转载于:https://www.cnblogs.com/chvv/p/10047339.html

你可能感兴趣的文章
计算机基础作业1
查看>>
Ubuntu 深度炼丹环境配置
查看>>
C#中集合ArrayList与Hashtable的使用
查看>>
从一个标准 url 里取出文件的扩展名
查看>>
map基本用法
查看>>
poj-1163 动态规划
查看>>
Golang之interface(多态,类型断言)
查看>>
Redis快速入门
查看>>
BootStrap---2.表格和按钮
查看>>
Linear Algebra lecture 2 note
查看>>
CRC计算模型
查看>>
Ajax之404,200等查询
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
csv HTTP简单表服务器
查看>>
OO设计的接口分隔原则
查看>>
数据库连接字符串大全 (转载)
查看>>
java类加载和对象初始化
查看>>
对于负载均衡的理解
查看>>
django简介
查看>>
window.event在IE和Firefox的异同
查看>>