Django viewsets 视图集与 router 路由实现评论接口开发

正常来说遵循restful风格编写接口,定义一个类包含了 get post delete put 四种请求方式,这四种请求方式是不能重复的
例如:获取单条记录和多条记录使用的方式都是get,如果两个都要实现的话那么得定义两个类,因为在同一个类中不能有两个get
这里继承ViewSetMixin来解决该问题(根据是否传参)

comment/views

注意ViewSetMixin继承顺序要放在第一个

python 复制代码
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \
    DestroyModelMixin
from rest_framework.viewsets import ViewSetMixin

from apps.comment.models import Comment
from apps.comment.serializers import CommentSerializer


# Create your views here.
class CommentGenericAPIView(ViewSetMixin,
                            GenericAPIView,
                            ListModelMixin,
                            CreateModelMixin,
                            RetrieveModelMixin,
                            UpdateModelMixin,
                            DestroyModelMixin
                            ):
    queryset = Comment.objects
    serializer_class = CommentSerializer

    def single(self,request,pk):
        print("我是查询一个")
        return self.retrieve(request,pk)

    def my_list(self,request):
        print("我是查询多个")
        return self.list(request)

    def edit(self,request,pk):
        print("我是更新")
        return self.update(request,pk)

    def my_save(self,request):
        print("我是保存")
        return self.create(request)

    def my_delete(self,request,pk):
        print("我是删除")
        return self.destroy(request,pk)

comment/urls

python 复制代码
from django.urls import path, re_path
from .views import CommentGenericAPIView

urlpatterns = [
    path("",CommentGenericAPIView.as_view({
        "get":"my_list",
        "post":"my_save"   #保存
    })),
    re_path("(?P<pk>.*)",CommentGenericAPIView.as_view(
        {
            "get":"single",
            "post":"edit",  #编辑
            "delete":"my_delete"
        }
    )),
]

comment/models

python 复制代码
from django.db import models

# Create your models here.
from django.db import models


class Comment(models.Model):
    user_id = models.IntegerField(blank=True, null=True)
    sku_id = models.CharField(max_length=255, blank=True, null=True)
    content = models.CharField(max_length=255, blank=True, null=True)
    user_image_url = models.CharField(max_length=255, blank=True, null=True)
    reference_name = models.CharField(max_length=255, blank=True, null=True)
    score = models.IntegerField(blank=True, null=True)
    nickname = models.CharField(max_length=255, blank=True, null=True)
    reply_count = models.CharField(max_length=255, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'comment'

comment/serializers.py

python 复制代码
from rest_framework import serializers

from apps.comment.models import Comment
from apps.goods.models import Goods



class CommentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Comment
        fields = "__all__"

viewsets 路由原理解析

ViewSetMixin中没有路由分发的方法,所以此处继承了GenericAPIView(View中有),不然不能进行路由的分发,继承APIView或者View都行

相关推荐
WangYaolove131410 小时前
基于opencv的疲劳检测系(源码+文档)
python·mysql·django·毕业设计·源码
luoluoal1 天前
基于python的人脸识别的酒店客房入侵检测系统(源码+文档)
python·mysql·django·毕业设计·源码
unable code1 天前
磁盘取证-Flying_High
网络安全·ctf·misc·1024程序员节·磁盘取证
Blossom.1181 天前
从数字大脑到物理实体:具身智能时代的大模型微调与部署实战
人工智能·python·深度学习·fpga开发·自然语言处理·矩阵·django
墨染青竹梦悠然1 天前
基于Django+vue的零食商城
python·django
unable code2 天前
磁盘取证-ColorfulDisk
网络安全·ctf·misc·1024程序员节·内存取证
WangYaolove13142 天前
基于Python的旅游城市关键词分析
python·django·毕业设计·源码·计算机源码
历程里程碑3 天前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
unable code3 天前
磁盘取证-[第十章][10.1.2 磁盘取证方法]磁盘取证1
网络安全·ctf·misc·1024程序员节·内存取证
DingYuan1013 天前
Django模板继承详解
django