如何覆盖Django中的默认用户模型字段? [英] How to override default user model field in Django?
问题描述
问题是默认的用户模型没有给电子邮件字段的 unique = True 字段提供一些非常有用的选项。
The problem is the default User model does not have some very useful options given to the fields e.g unique=True to the email field.
我读了这个问题:覆盖默认用户模型方法,并勾选<一个href =http://docs.djangoproject.com/en/dev/topics/db/models/#id8 =nofollow noreferrer>代理模型概念,但不起作用。
I read this question: Override default User model method, and checked Proxy Model concept, but with no effect.
起初我试过:
from django.contrib.auth import models
class User(models.User):
class Meta:
proxy = True
email = EmailField('e-mail address', unique=True, blank=False)
导致:
django.core.exceptions.FieldError: Proxy model 'User' contains model fields.
所以下一个是:
from django.contrib.auth import models
class User(models.User):
class Meta:
proxy = True
models.User.email = EmailField('e-mail address', unique=True, blank=False)
解决方案根本没有任何效果。默认用户模型的行为与以前一样。
and this "solution" has no effect at all. Default User model was behaving the same as before.
我只对非猴子补丁解决方案感兴趣。
I am interested in non-monkey-patching-solution only.
编辑:好的。猴子补丁对我来说是可以接受的,但是如何合理呢? (我的意思是不要更改位于 /usr/pyshared/python2.6/...
中的文件)
Ok. Monkey-patching is acceptable for me, but how make this reasonably? (I mean not changing the file that lies in /usr/pyshared/python2.6/...
)
推荐答案
已经有这个问题了... 如何使模型中的电子邮件字段独一无二Django中的contrib.auth中的用户
There's this already asked... How to make email field unique in model User from contrib.auth in Django
另外其他用户数据的django方法在这里是 http://docs.djangoproject .com / en / dev / topics / auth /#auth-profiles 。
Also the django method of additional user data is here http://docs.djangoproject.com/en/dev/topics/auth/#auth-profiles .
我不完全是这种方法的粉丝,并且欣赏你来自哪里,但有时候你必须和牧群一起去。
I'm not entirely a fan of this method and appreciate where you are coming from, but sometimes you have to go with the herd.
这篇关于如何覆盖Django中的默认用户模型字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!