【问题标题】:django two type of accountsdjango 两种账户类型
【发布时间】:2014-10-18 06:59:40
【问题描述】:

我想在 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:类中的本地字段“密码” 'CustomUser' 与基类中类似名称的字段冲突 '抽象用户'

为此,我将删除用户个人资料中的所有内容。

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 中创建两种类型的帐户?

已编辑 在 admin 中给出 AUTH_USER_MODEL 后,这里又出现了一个错误。

CommandError:一个或多个模型未验证: admin.logentry: 'user' 与模型 firstapp.CustomUser 有关系,该模型要么尚未安装,要么是抽象的。 auth.user:模型已被替换为尚未安装或抽象的“firstapp.CustomUser”。

【问题讨论】:

    标签: python django django-models django-admin


    【解决方案1】:

    我将您在代码中的错误标记为 cmets:

    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 threecomment four 在这里是不必要的,因为 django auth 用户模型提供了这些(电子邮件/密码)。

    第二部分:

    class CustomUser(AbstractUser):
        pass
    

    您必须在 settings.py 中声明 AUTH_USER_MODEL。在你的情况下:

    AUTH_USER_MODEL = 'your_app.CustomUser'
    

    【讨论】:

    • 哦 .,.. 现在我在设置中给出了 AUTH_USER_MODEL,我收到一个命令错误...我用那个错误更新我的问题
    • 检查firstapp是否在INSTALLED_APPS中,其次请确保您的模型不是抽象的(添加字段而不是使用pass)@user3895077
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2018-01-16
    • 2023-01-21
    • 2021-12-02
    • 2013-05-16
    • 1970-01-01
    • 2021-04-01
    相关资源
    最近更新 更多