zoukankan      html  css  js  c++  java
  • 自关联、中间件

    M2M自关联特性:

    class UserInfo(models.Model):
        nickname = models.CharField(max_length=32)
        username = models.CharField(max_length=32)
        password = models.CharField(max_length=64)
        gender_choices = (
            (1,''),
            (2,''),
        )
        gender = models.IntegerField(choices=gender_choices)
        m = models.ManyToManyField('UserInfo')
        
    会额外生成一张表,userinfo_m   里面有三列id 、from_userinfo_id、to_userinfo_id
    M2M自关联特性:
        obj = models.UserInfo.objects.filter(id=1).first()
        # from_userinfo_id
        obj.m          => select xx from xx where from_userinfo_id = 1
        
        # to_userinfo_id
        obj.userinfo_set => select xx from xx where to_userinfo_id = 1    
    定义:
        # 前面列:男生ID
        # 后面列:女生ID
    查男生
    xz = models.UserInfo.objects.filter(id=1).first()
    u = xz.m.all()   
    for row in u:
        print(row.nickname)
    查女神
    xz = models.UserInfo.objects.filter(id=4).first()
    v = xz.userinfo_set.all()
    for row in v:
        print(row.nickname)                

    FK自关联:

    class Comment(models.Model):
        """
        评论表
        """
        news_id = models.IntegerField()            # 新闻ID
        content = models.CharField(max_length=32)  # 评论内容
        user = models.CharField(max_length=32)     # 评论者
        reply = models.ForeignKey('Comment',null=True,blank=True,related_name='xxxx')
        #blank   Admin中是否允许用户输入为空
    
    """
       新闻ID                         reply_id
    1   1        别比比    root         null
    2   1        就比比    root         null
    3   1        瞎比比    shaowei      null
    4   2        写的正好  root         null
    5   1        拉倒吧    由清滨         2
    6   1        拉倒吧1    xxxxx         2
    7   1        拉倒吧2    xxxxx         5
    """
    """
    新闻1
        别比比
        就比比
            - 拉倒吧
                - 拉倒吧2
            - 拉倒吧1
        瞎比比
    新闻2:
        写的正好
    """

    中间件

    - 应用:对所有请求或一部分请求做批量处理

    在配置setting中MIDDLEWARE就是中间件
    创建一个文件m1.py
    from django.utils.deprecation import MiddlewareMixin
    
    from django.shortcuts import HttpResponse
    class Middle1(MiddlewareMixin):
        def process_request(self, request):
            print('m1.process_request')        结果直接传递给下一个中间件。
            # return HttpResponse('不要再往下传了')
            如果有返回值,就不再传递给下一个中间件了。
    
        def process_response(self, request, response):
            print('m1.process_response')
            return response   必须要返回,不然下一个中间件不能拿到结果。
    
    
    在配置setting中MIDDLEWARE添加中间件m1
    “m1. Middle1”

     中间件的分类和执行顺序:

    process_request    
        如果有返回值,1.10版本以后就从当前中间件执行process_response,1.10版本以前,从最后一个中间件执行process_response
    process_view    找到路由匹配,如果有返回值,从最后一个中间件执行process_response
    test:
    自己在视图函数做封装:
        class JSONResponse:
            def __init__(self,req,status,msg):
                self.req = req
                self.status = status
                self.msg = msg
            def render(self):
                import json
                ret = {
                    'status': self.status,
                    'msg':self.msg
                }
                return HttpResponse(json.dumps(ret))
    
        def test(request):                    
            return JSONResponse(request,True,"错误信息")
    process_template_view  如果视图函数有render返回值才执行                    
    process_exception       有错误才执行,然后再执行process_response,如果有返回值Httpresponse处理了异常,就不再处理后面的异常了,直接执行process_response
    process_response
        必须有返回值
        return response    
    View Code
    from django.utils.deprecation import MiddlewareMixin
    from django.shortcuts import HttpResponse
    class M1(MiddlewareMixin):
        def process_request(self,request):
            print('m1.process_request')
    
        def process_view(self, request, callback, callback_args, callback_kwargs):
            print('m1.process_view')
            # response = callback(request,*callback_args,**callback_kwargs)
            # return response
    
        def process_response(self,request,response):
            print('m1.process_response')
            return response
    
        def process_exception(self, request, exception):
            print('m1.process_exception')
    
        def process_template_response(self,request,response):
            """
            视图函数的返回值中,如果有render方法,才被调用
            :param request:
            :param response:
            :return:
            """
            print('m1.process_template_response')
            return response
    举例
  • 相关阅读:
    初学python 遇到的坑
    golang 裸写一个pool池控制协程的大小
    自制C#版3DS文件的解析器并用SharpGL显示3DS模型
    [译+改]最长回文子串(Longest Palindromic Substring) Part II
    [译]最长回文子串(Longest Palindromic Substring) Part I
    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之纹理Textures
    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes
    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格渲染器和过滤器Mesh renderers and filters
    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials
    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights
  • 原文地址:https://www.cnblogs.com/domestique/p/7107452.html
Copyright © 2011-2022 走看看