django拓展用户proxy代理

Posted harrytree

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于django拓展用户proxy代理的主要内容,如果未能解决你的问题,请参考以下文章

django xadmin拓展User模型

ubuntu12.04+apache2.4+django 配置反向代理

Django获取request中的真实IP

如何拓展Django内置的contrib.auth.models.User

Nginx Serve React 构建和 proxy_pass Django Rest api 服务器

django drf框架中的user验证以及JWT拓展的介绍