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 Files\python3.6.7\python.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

posted on 2019-11-04 18:58  芦苇草鱼  阅读(644)  评论(0编辑  收藏  举报