zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    PHP笔试题
    找工作的几种方式
    ThinkPHP3.2.3学习笔记5---模板(一)
    PHP7新特性
    了解Web Uploader
    什么是云购网
    使用PDO操作数据库的好处
    MySQL与MongoDB的区别
    显示和编辑注解
    自定义验证逻辑
  • 原文地址:https://www.cnblogs.com/harryTree/p/11794018.html
Copyright © 2011-2022 走看看