我想在派生类中重新定义class属性。电子邮件属性已存在于父类User
中,但它根本不需要unique=True
属性。我知道Django不直接支持这个功能。所以我这样做了:
class CustomUser(User):
"""The inherited User class. Email field are redefined to add the warning
about uniqueness is required. UserManager as the default manager so that
the standard methods are available."""
def __init__(self, *args, **kwargs):
super(CustomUser, self).__init__(*args, **kwargs)
email = models.EmailField(unique=True)
email.contribute_to_class(u'email', self)
objects = UserManager()
回溯:
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/options.py" in wrapper
307. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/sites.py" in inner
196. return self.login(request)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/sites.py" in login
331. return login(request, **defaults)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/auth/views.py" in login
35. if form.is_valid():
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in is_valid
121. return self.is_bound and not bool(self.errors)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in _get_errors
112. self.full_clean()
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in full_clean
268. self._clean_form()
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/forms/forms.py" in _clean_form
296. self.cleaned_data = self.clean()
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/admin/forms.py" in clean
26. self.user_cache = authenticate(username=username, password=password)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/contrib/auth/__init__.py" in authenticate
55. user = backend.authenticate(**credentials)
File "/home/i159/workspace/students/backends.py" in authenticate
15. print self.user_class()
File "/home/i159/workspace/students/stdapp/models.py" in __init__
13. email.contribute_to_class(u'email', self)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in contribute_to_class
235. self.set_attributes_from_name(name)
File "/home/i159/Envs/students/lib/python2.6/site-packages/django/db/models/fields/__init__.py" in set_attributes_from_name
232. self.verbose_name = name.replace('_', ' ')
Exception Type: AttributeError at /admin/stdapp/customuser/add/
Exception Value: 'CustomUser' object has no attribute 'replace'
我该怎么做才能解决它或如何在unique
字段中将True
设置为email
?这个例外的原因是什么?
另一种方法。
第一个变化:
class CustomUser(User):
def __init__(self, *args, **kwargs):
setattr(self._meta.fields[4], 'unique', True)
第二
class CustomUser(User):
def __init__(self, *args, **kwargs):
self._meta.fields[4].unique=True
两者都有相同的错误:can't set attribute
。