我在Django 1.5.4 (稳定的)中没有错误,但是当我从正式的tar.gz上在Django 1.6 beta 4上测试我的应用程序时,我在启动时发现了验证模型的错误。
models.py
from django.contrib.auth.models import AbstractUser, User
class ShopUser(AbstractUser):
model_car = models.CharField(max_length=200)
date_car = models.DateField()
description = models.TextField(blank=True, db_index=True)
manager = models.ForeignKey(User)
这是manage.py runserver控制台日志:
Validating models...
Unhandled exception in thread started by <function wrapper at 0x2d941b8>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 93, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 97, in inner_run
self.validate(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 312, in validate
raise CommandError("One or more models did not validate:\n%s" % error_text)
django.core.management.base.CommandError: One or more models did not validate:
adminka.shopuser: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
adminka.shopuser: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.
auth.user: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
auth.user: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.
要解决这个问题需要做些什么?
发布于 2014-02-26 22:54:16
您必须在您的AUTH_USER_MODEL settings.py上声明settings.py。就你而言:
AUTH_USER_MODEL = 'your_app.ShopUser'
发布于 2014-09-22 18:58:58
对我来说,这是在Django 1.6.5中通过更改以下内容来修正的:
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
pass
对此:
from django.contrib.auth.models import AbstractBaseUser
class CustomUser(AbstractBaseUser):
pass
Django 1.6.5不需要进行其他修改。@Stormlifter的建议有其可取之处,但我通过python auth将这个CustomUser与OAuth2一起使用:
$ pip freeze
Django==1.6.5
Markdown==2.4.1
MySQL-python==1.2.5
PyJWT==0.2.1
South==1.0
basicauth==0.2
boto==2.28.0
django-filter==0.7
django-guardian==1.2.0
django-storages==1.1.8
djangorestframework==2.3.14
httplib2==0.9
oauth2==1.5.211
oauthlib==0.6.3
python-memcached==1.53
python-oauth2==0.7.0
python-openid==2.2.5
python-social-auth==0.2.1
requests==2.4.1
requests-oauthlib==0.4.1
shortuuid==0.4.2
six==1.7.2
wsgiref==0.1.2
正如@jordiburgos建议的那样,我的新CustomUser将比默认用户做得更多,并且确实需要成为settings.py
中的AUTH_USER_MODEL='myapp.CustomUser'
。
我希望这能帮到你!
发布于 2013-10-01 18:07:40
您希望从AbstractBaseUser
继承,而不是从AbstractUser
继承。
但是,看起来您只是想为用户存储一些附加信息,我不建议为此使用自定义用户模型。利用人际关系。
class ShopUser(models.Model):
model_car = models.CharField(max_length=200)
date_car = models.DateField()
description = models.TextField(blank=True, db_index=True)
user = models.OneToOneField(User)
如果您需要多个ShopUser来关联单个用户,您可以将其变成一个ForeignKey,但我认为OneToOne最有意义。
如Django文件所述:
https://stackoverflow.com/questions/19120527
复制相似问题