【问题标题】:Django: INSTALLED_APPS naming conflict?Django:INSTALLED_APPS 命名冲突?
【发布时间】:2015-12-08 14:32:26
【问题描述】:

按照文档的建议,我在自己的应用中创建了自定义用户身份验证。它被命名为UserAuth。但是,我还有一个Users 应用程序,它处理项目的各种用户角色。

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'AlmondKing.UserAuth',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.twitter',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.linkedin',
    'formtools',
]

AUTH_USER_MODEL = 'UserAuth.AKUser'

这很好用,但是当我将AlmondKing.Users 添加到INSTALLED_APPS 时,它会中断:

ERRORS:
Users.ManagerAccount.user: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
        HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
Users.CustomerAccount.user: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
        HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
Users.FranchiseAccount.user: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
        HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.

当我尝试使用runserver 时,会打印出上述内容。它指的是Users应用程序中的三个模型。

我违反了什么规则?

【问题讨论】:

  • 我猜问题出在您指向django.contrib.auth.models.User的某些模型中
  • 啊,是的!哇。我只需要告诉模型使用新的 AKUser

标签: python django django-models django-settings


【解决方案1】:

如果您使用 FK 到 django.contrib.auth.models.User,请检查您的模型,并将其替换为指向您自己的模型 UserAuth.AKUser

【讨论】:

  • 在这种情况下,我在扩展主用户时使用 OnetoOneField。
  • 考虑使用在django.conf.settings.AUTH_USER_MODEL上制作FK,这样非用户模型就不必知道使用的是哪个模型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 2010-11-15
  • 2012-12-18
相关资源
最近更新 更多