码迷,mamicode.com
首页 > Web开发 > 详细

Drf03 /呼啦圈网页初步实现、drf筛选、视图(源码实现)

时间:2019-11-09 21:27:29      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:class   add   ret   筛选条件   img   filters   val   put   字段   

Drf03 /呼啦圈网页初步实现、drf筛选、视图(源码实现)

内容回顾与补充

  1. restful规范

    - URL中一般用名词: 
     http://www.luffycity.com/article/ (面向资源编程,网络上东西都视为资源)
    - 根据请求不同做不同操作:GET/POST/PUT/DELTE/PATCH
    - 筛选条件,在URL参数中进行传递:
     http://www.luffycity.com/article/?page=1&category=1
    
    一般传输的数据格式都是JSON
    持续更新...
  2. 潜规则:类的约束

  3. drf的配置

    setting.py
    
    REST_FRAMEWORK = {
     ...
    }
  4. 分页

    • page
    • offset

今日详细

1.呼啦圈初步实现

1.1 表结构设计

  • 不会经常变化的值放在内存:choices形式,避免跨表性能低。
  • 分表:如果表中列太多/大量内容可以选择水平分表
  • 表自关联

技术图片

from django.db import models

class UserInfo(models.Model):
    """ 用户表 """
    username = models.CharField(verbose_name='用户名',max_length=32)
    password = models.CharField(verbose_name='密码',max_length=64)


class Article(models.Model):
    """ 文章表 """
    category_choices = (
        (1,'咨询'),
        (2,'公司动态'),
        (3,'分享'),
        (4,'答疑'),
        (5,'其他'),
    )
    category = models.IntegerField(verbose_name='分类',choices=category_choices)
    title = models.CharField(verbose_name='标题',max_length=32)
    image = models.CharField(verbose_name='图片路径',max_length=128) # /media/upload/....
    summary = models.CharField(verbose_name='简介',max_length=255)

    comment_count = models.IntegerField(verbose_name='评论数',default=0)
    read_count = models.IntegerField(verbose_name='浏览数',default=0)

    author = models.ForeignKey(verbose_name='作者',to='UserInfo')
    date = models.DateTimeField(verbose_name='创建时间',auto_now_add=True)

class ArticleDetail(models.Model):
    article = models.OneToOneField(verbose_name='文章表',to='Article')
    content = models.TextField(verbose_name='内容')


class Comment(models.Model):
    """ 评论表 """
    article = models.ForeignKey(verbose_name='文章',to='Article')
    content = models.TextField(verbose_name='评论')
    user = models.ForeignKey(verbose_name='评论者',to='UserInfo')
    # parent = models.ForeignKey(verbose_name='回复',to='self', null=True,blank=True)
    # 自关联  --- 评论-->回复-->回复

1.2 系统结构(CMS)

技术图片

1.3 功能实现

1.3.1 增加文章(可以不写)

一次增加两个表中的数据:

    def post(self,request,*args,**kwargs):
        """ 新增文章(应该在后台管理开发)"""
        ser = ArticleSerializer(data=request.data)
        ser_detail = ArticleDetailSerializer(data=request.data)
        if ser.is_valid() and ser_detail.is_valid():
            # 增加文章
            article_object = ser.save(author_id=1)
            ser_detail.save(article=article_object)
            return Response('添加成功')
        return Response('错误')

1.3.2 文章列表

1.3.3 文章详细

1.3.4 评论列表

  • 查看评论列表
    访问时:http://127.0.0.1:8000/hg/comment/?article=2

  • 添加评论

    http://127.0.0.1:8000/hg/comment/
    
    {
      article:1,
      content:'xxx'
    }
    http://127.0.0.1:8000/hg/comment/?article=1
    
    {
      content:'xxx'
    }

2. 筛选

案例:在文章列表时候,添加筛选功能。

全部:http://127.0.0.1:8000/hg/article/
筛选:http://127.0.0.1:8000/hg/article/?category=2
class ArticleView(APIView):
    """ 文章视图类 """

    def get(self,request,*args,**kwargs):
        """ 获取文章列表 """
        pk = kwargs.get('pk')
        if not pk:
            condition = {}
            category = request.query_params.get('category')
            if category:
                condition['category'] = category
            queryset = models.Article.objects.filter(**condition).order_by('-date')
            
            pager = PageNumberPagination()
            result = pager.paginate_queryset(queryset,request,self)
            ser = ArticleListSerializer(instance=result,many=True)
            return Response(ser.data)
        article_object = models.Article.objects.filter(id=pk).first()
        ser = PageArticleSerializer(instance=article_object,many=False)
        return Response(ser.data)
drf的组件:内置了筛选的功能
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models

from rest_framework.filters import BaseFilterBackend

class MyFilterBackend(BaseFilterBackend):

    def filter_queryset(self, request, queryset, view):
        val = request.query_params.get('cagetory')
        return queryset.filter(category_id=val)
    

class IndexView(APIView):

    def get(self,request,*args,**kwargs):
        # http://www.xx.com/cx/index/
        # models.News.objects.all()

        # http://www.xx.com/cx/index/?category=1
        # models.News.objects.filter(category=1)

        # http://www.xx.com/cx/index/?category=1
        # queryset = models.News.objects.all()
        # obj = MyFilterBackend()
        # result = obj.filter_queryset(request,queryset,self)
        # print(result)
        
        return Response('...')

3.视图

  • APIView,感觉没提供功能。

  • GenericAPIView,桥梁,内部定义:get_queryset/get_serilizer/get_page...

  • ListAPIView,CreateAPIView,RetrieveAPIView,UpdateAPIView,DestroyAPIView
class TagSer(serializers.ModelSerializer):
    class Meta:
        model = models.Tag
        fields = "__all__"

class TagView(ListAPIView,CreateAPIView):
    queryset = models.Tag.objects.all()
    serializer_class = TagSer

    def get_serializer_class(self):
        # self.request
        # self.args
        # self.kwargs
        if self.request.method == 'GET':
            return TagSer
        elif self.request.method == 'POST':
            return OtherTagSer
    def perform_create(self,serializer):
        serializer.save(author=1)

class TagDetailView(RetrieveAPIView,UpdateAPIView,DestroyAPIView):
    queryset = models.Tag.objects.all()
    serializer_class = TagSer

技术图片

总结:

1.ser.save(author=1)
  # 没有在serializer校验的字段提交到数据库
  
2.  content = serializers.CharField(source='articledetail.content')
    author = serializers.CharField(source='author.username')
    category = serializers.CharField(source='get_category_display')
    date = serializers.SerializerMethodField()
    class Meta:
        model = models.Article
        fields = '__all__'
    def get_date(self,obj):
        obj.date.strftime('%Y-%m-%d %H:%M')
        
    # __all__,可以将source等字段,都显示出来
    # 格式化时间strftime

Drf03 /呼啦圈网页初步实现、drf筛选、视图(源码实现)

标签:class   add   ret   筛选条件   img   filters   val   put   字段   

原文地址:https://www.cnblogs.com/liubing8/p/11827591.html

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