【问题标题】:django auth ldap with custom user modeldjango auth ldap 与自定义用户模型
【发布时间】:2014-01-30 13:38:45
【问题描述】:

我正在尝试将 django auth ldap (v1.1.7) 与自定义用户模型一起使用,虽然我已经处理了所有其他事情,但我仍然遇到了一个熟悉的错误消息,我只是不知道如何处理修复。

运行python manage.py syncdb 返回:

CommandError: One or more models did not validate:
django_auth_ldap.testprofile: 'user' defines a relation with the model 'auth.Use
r', which has been swapped out. Update the relation to point at settings.AUTH_US
ER_MODEL.

我的问题是 - 我在哪里可以找到 django_auth_ldap.testprofile 并更新关系以解决问题?

提前感谢您帮助我。

L.E.:好的,经过一番谷歌搜索,我发现这个 testprofile 位于 django_auth_ldap/models.py 并得到这个:我搜索了我的 所有 文件并找到了修改后的文件它指向 settings.AUTH_USER_MODEL ,但我仍然收到相同的错误。

谁能帮我解决这个问题?

【问题讨论】:

  • 您的自定义用户模型在哪里? settings.py 中AUTH_USER_MODEL 的值是多少?
  • 我的自定义用户模型位于 projects/models.py 中,名为 CustomUser(projects 是我的应用程序的名称)。在设置中,AUTH_USER_MODEL 的值为 projects.CustomUser。当我将外键定义为指向 settings.AUTH_USER_MODEL 时,所有其他关系都已修复。

标签: python django django-auth-ldap


【解决方案1】:

这是django_auth_ldap 的models.py 中的the part -

class TestProfile(models.Model):
    """
    A user profile model for use by unit tests. This has nothing to do with the
    authentication backend itself.
    """
    user = models.OneToOneField('auth.User')  # <-- here is the trouble
    is_special = models.BooleanField(default=False)
    populated = models.BooleanField(default=False)

除非你修复这部分代码并按照Django documentation中所说的那样做,否则你无法修复它 -

您应该引用用户,而不是直接引用用户 模型使用 django.contrib.auth.get_user_model()。这种方法将 返回当前活动的用户模型——如果是自定义用户模型 指定,否则指定用户。

我不建议自己修改第 3 方包。如果可以,请向开发人员建议更改,或向他们发送拉取请求。接受后,更新包。

【讨论】:

  • 这看起来很有希望 - 让您随时了解结果
  • 很奇怪,现在我遇到了错误CommandError: One or more models did not validate:django_auth_ldap.testprofile: 'user' has a relation with model settings.AUTH_USER_MODEL, which has either not been installed or is abstract.
  • 设法让它工作。最初我有user = models.OneToOneField('settings.AUTH_USER_MODEL'),但我将其更改为user = get_user_model(),之后它工作正常。太糟糕了,我不得不对第 3 方包进行锻炼。非常感谢您的帮助
  • 我仍然建议您向软件包开发人员发送拉取请求。否则,您将永远失去依赖关系。
猜你喜欢
  • 1970-01-01
  • 2013-02-07
  • 2023-03-04
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2016-04-30
  • 2016-07-02
相关资源
最近更新 更多