我没有使用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'.
答案 0 :(得分:15)
您必须在 settings.py 上声明 AUTH_USER_MODEL 。在你的情况下:
AUTH_USER_MODEL = 'your_app.ShopUser'
答案 1 :(得分:1)
对我来说,通过改变这个来修复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-social-auth将这个东西用于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
我的新CustomUser将比默认用户做得更多,并且需要成为AUTH_USER_MODEL='myapp.CustomUser'
中settings.py
的@jordiburgos建议。
我希望这有帮助!
答案 2 :(得分:0)
您希望继承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 Documents所说: