简体   繁体   English

扩展Django Auth用户模型

[英]Extending Django Auth User model

admin.py admin.py

class NetUserInline(admin.StackedInline):
    model = NetUser

class UserProfileAdmin(UserAdmin):
    inlines = [NetUserInline, ]

admin.site.register(User, UserProfileAdmin)

models.py models.py

class NetUser(AbstractUser):
    Net_30 = models.BooleanField(default=False)

site.register(NetUser)

im getting error like this. 我得到这样的错误。

django.core.management.base.CommandError: One or more models did not validate:
accounts.netuser: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
accounts.netuser: 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'.

The Django User model is automatically part of any Django project. Django用户模型自动成为任何Django项目的一部分。 That is to say that the User model will create all database tables that are part of it at the same time you run syncdb . 也就是说,用户模型将在运行syncdb的同时创建所有数据库表,该数据库表是其中的一部分。 You have subclassed AbstractUser for your NetUser class, meaning that all the fields that are part of AbstractUser are part of NetUser as well. 您已将NetUser类的AbstractUser子类化,这意味着AbstractUser中的所有字段也都是NetUser的一部分。 So, right now your database has tables such as this... 因此,现在您的数据库具有这样的表...

  • User 用户
    • username 用户名
    • password 密码
    • email 电子邮件
    • ... ...
  • NetUser NetUser
    • username 用户名
    • password 密码
    • email 电子邮件
    • ... ...
    • net_30 net_30

As part of Django's user administration, database tables are created that manage what groups/permissions users have within your application(s). 作为Django用户管理的一部分,将创建数据库表来管理用户在您的应用程序中具有的组/权限。 Right now it appears that this system is attempting to connect to both the standard User model and your NetUser model. 现在看来,该系统正在尝试连接到标准用户模型和您的NetUser模型。 I'd assume this is because by subclassing AbstractUser Django attempts to create all relations to that user model. 我认为这是因为Django通过将AbstractUser子类化,试图创建与该用户模型的所有关系。 At the same time, if you haven't specified that you are using a custom user model then Django will be attempting to do the same to the standard user model. 同时,如果您尚未指定使用的是自定义用户模型,则Django将尝试对标准用户模型执行相同的操作。

The documentation states you must specify what you are using for a user model if not the default by adding AUTH_USER_MODEL = 'Accounts.NetUser' to your settings.py file. 文档指出,如果不是默认值,则必须通过将AUTH_USER_MODEL = 'Accounts.NetUser'添加到settings.py文件中来指定用于用户模型的内容。 This tells your Django project that you are using a custom User model and not connect to the standard User model. 这告诉您的Django项目您正在使用自定义用户模型,但未连接到标准用户模型。 Right now both are happening. 现在两者都在发生。

Furthermore, you should change admin.site.register(User, UserProfileAdmin) to admin.site.register(NetUser, UserProfileAdmin) so that your Admin backend is looking at your custom User model and not the standard Django User model. 此外,您应该将admin.site.register(User, UserProfileAdmin)更改为admin.site.register(NetUser, UserProfileAdmin)以便您的Admin后端查看您的自定义User模型而不是标准Django User模型。

You probably forgot to set AUTH_USER_MODEL in settings.py 您可能忘记了在settings.py中设置AUTH_USER_MODEL

AUTH_USER_MODEL = 'myapp.NetUser'

docs: substituting-a-custom-user-model docs: 替代自定义用户模型

PS: However using some kind of profile imho is generally less painful ) PS:但是,使用某种配置文件恕我直言通常不会那么痛苦)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM