我想在django 1.6中创建两种类型的用户帐户
所以我正在学习一个教程MULTIPLE USER TYPES IN DJANGO >=1.5
from django.db import models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
user = models.OneToOneField(User)
password = models.CharField(
email = models.EmailField(blank=True)
class LinkedInUser(CustomUser):
linkedin_id = models.CharField(max_length=30, unique=True)
class Meta:
verbose_name = 'LinkedIn User'
class FacebookUser(CustomUser):
facebook_id = models.CharField(max_length=30, unique=True)
class Meta:
verbose_name = 'Facebook User'
现在我收到的错误是:
django.core.exceptions.FieldError:类中的本地字段“password” 'CustomUser'与基类中类似名称的字段冲突 'AbstractUser'
因为我要删除用户个人资料中的所有内容。
class CustomUser(AbstractUser):
pass
但现在错误是:
(env)refei@user-desktop:~/studio/myproject$ python manage.py syncdb
CommandError: One or more models did not validate:
frontend.profile: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
frontend.profile: 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'.
你能指导我,我错了吗?如何在django 1.6中创建两种类型的帐户?
被修改 在管理员给出AUTH_USER_MODEL之后是另一个错误。
CommandError:一个或多个模型未验证: admin.logentry:'user'与模型firstapp.CustomUser有关系,它已经安装或者是抽象的。 auth.user:模型已被换出'firstapp.CustomUser',它尚未安装或是抽象的。
答案 0 :(得分:1)
我在代码中将您的错误标记为注释:
class CustomUser(AbstractUser): # one
user = models.OneToOneField(User) # two
password = models.CharField(max_length=30) # three
email = models.EmailField(blank=True) # four
在comment one
您正在扩展AbstructUser
模型,那么为什么要在模型中添加用户作为OneToOne关系(in comment two
),您必须使用其中任何一个。检查:https://docs.djangoproject.com/en/dev/topics/auth/customizing/
comment three
和comment four
,因为django auth用户模型提供了那些(电子邮件/密码)。
第二部分:
class CustomUser(AbstractUser):
pass
您必须在settings.py上声明AUTH_USER_MODEL。在你的情况下:
AUTH_USER_MODEL = 'your_app.CustomUser'