• django拓展用户proxy代理



    1.app0 1 /models.py里面定义User代理模型Person.

    from django.db import models
    from django.contrib.auth.models import User

    class Person(User):
    class Meta:
    proxy = True

    @classmethod
    def get_blacklist(cls):
    return cls.objects.all()

    说明:

    代理模型不能定义models模型字段,比如,Person里面加一个telephone字段
    from django.contrib.auth.models import User

    # Create your models here.
    class Person(User):
    telephone = models.CharField(max_length=11)
    class Meta:
    proxy = True

    @classmethod
    def get_blacklist(cls):
    return cls.objects.all()

    执行makemigrations会报错

    "D:Program Filespython3.6.7python.exe" D : /pythonWorkspac e /untitled101 9 /manage.py makemigrations
    SystemCheckError: System check identified some issues:

    ERRORS:
    ?: (models.E017) Proxy model 'Person' contains model fields.

    Process finished with exit code 1

    总结:

    代理模型使用很有限,不能新增字段,只能自定义一些属性和方法



    2.app0 1 /views.py视图调用代理模型person
    from django.shortcuts import render, HttpResponse

    from app01.models import Person


    def test(request):
    blacklist = Person.get_blacklist()
    for person in blacklist:
    print(person.username)
    return HttpResponse("proxy")





    3、浏览器访问http :/ /127.0.0.1 :808 0 /tes t /后,打印结果如下:
    System check identified no issues (0 silenced).
    November 04, 2019 - 18 :30:43
    Django version 2.
    2.2, using settings 'untitled1019.settings'
    Starting development server at http :/ /127.0.0.1 :808 0/
    Quit the server with CTR L -BREAK.
    zhiliao
    zhiliao2

  • 相关阅读:
    数据库三范式(转)
    Tyrion中文文档(含示例源码)
    mongodb数据库导入导出恢复
    HTTP协议:Content-Type
    requests爬虫组件
    JS 数组对象
    JS 函数
    javascript window对象属性和方法
    js String对象
    Math对象-JavaScript
  • 原文地址:https://www.cnblogs.com/harryTree/p/11794018.html
Copyright © 2020-2023  润新知