【问题标题】:AUTH_PROFILE_MODULE value in django when the models is not a file but a directory当模型不是文件而是目录时,Django 中的 AUTH_PROFILE_MODULE 值
【发布时间】:2026-02-14 12:25:02
【问题描述】:

我有以下:(它按预期工作)

# In <..>/profile/models.py
class UserProfile()
#In settings.py
AUTH_PROFILE_MODULE = "profile.UserProfile"

我有以下:(不工作)

# In <..>/profile/models/__init__.py
class UserProfile()
AUTH_PROFILE_MODULE = "profile.UserProfile"

似乎 get_profile() 调用了 get_model,它正在寻找 models.py 作为文件并尝试加载它。

这是错误:

raise SiteProfileNotAvailable('无法加载配置文件' SiteProfileNotAvailable:无法加载配置文件模型,请检查项目设置中的 AUTH_PROFILE_MODULE

原因是我在个人资料应用中有很多类,它们都在不同的文件中并导入:

<..>/profile/models/__init__.py

这适用于除 get_profile() 之外的所有内容。

任何解决方法的提示?

【问题讨论】:

  • 您提到您已将 AUTH_PROFILE_MODULE = "profile.UserProfile" 移至模块 <..>/profile/models/__init__.py 但您是否也在 settings.py 模块中保留了一个?导入设置并查看 settings.AUTH_PROFILE_MODULE 是否已定义并具有您期望的值。

标签: django django-models django-profiles


【解决方案1】:

有时 django 会对应用名称感到困惑,因此请确保您有:

class UserProfile(Model):
    ....
    class Meta:
        app_label = 'profile'

这将确保可以使用 profile.UserProfile 查找配置文件

【讨论】:

  • 这是“app_label”而不是“app_name”。