You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django-CMS结合自定义用户模型报错:Model 'accounts.CustomUser'未注册

问题:Django-CMS添加PlaceholderField触发LookupError: Model 'accounts.CustomUser' not registered

环境:Django 3.2 + Django-CMS 3.11

在events.models.EventVenue模型中添加cms.models.fields.PlaceholderField后,出现用户模型未注册的错误。已尝试调整INSTALLED_APPS顺序、让自定义用户类继承PermissionsMixin,问题未解决。


相关代码

1. EventVenue模型

from cms.models.fields import PlaceholderField
class EventVenue(models.Model):
    name = models.CharField(_('name'), max_length=255)
    slider = PlaceholderField('room_slider')

2. 配置信息

INSTALLED_APPS = [
    'modeltranslation',
    'djangocms_admin_style',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.forms',
    
    'haystack',
    'accounts',
    'cms',
    'menus',
    'treebeard',
    'sekizai',
    'filer',
    'easy_thumbnails',
    'mptt',
    'simple_history',
    'crispy_forms',
    'crispy_bootstrap5',

    'djangocms_text_ckeditor',
    'djangocms_link',
    'djangocms_file',
    'djangocms_picture',
    'djangocms_video',
    'djangocms_googlemap',
    'djangocms_snippet',
    'djangocms_style',
    'djangocms_forms',
    'django_flatpickr',
    'phonenumber_field',

    'bgv_cms',
    'ddz_cms',
    'events',
    'mail_list',
]
AUTH_USER_MODEL = 'accounts.CustomUser'

3. 自定义用户类

class CustomUser(AbstractUser):
    organization = models.ForeignKey(Organization, on_delete=models.SET_NULL,blank=True, null=True, related_name='employee')
    mailing_list = models.ManyToManyField(to='MailList', related_name='mail_list', blank=True, verbose_name=_('Mailing lists'))
    room = models.CharField(_('Room Mailing Lists'), max_length=100, choices=FLOOR, blank=True, null=True)
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ('username',)

    objects = CustomUserManager()

    def __str__(self):
        return self.get_full_name() or self.username

4. Organization类

class Organization(models.Model):
    name = models.CharField(max_length=255)
    contact_person = models.ForeignKey(settings.AUTH_USER_MODEL, 
                                       on_delete=models.CASCADE,
                                       blank=True, null=True, 
                                       help_text=_('if you changed the contact person to another user, you lose access to this organization info.'),
                                       related_name='owner')
    responsible_for_room = models.ForeignKey(EventVenue, on_delete=models.SET_NULL,blank=True, null=True, related_name='responsible_for_room')
    def __str__(self):
        return self.name

报错回溯

from events.models import EventVenue
  File "C:\Users\SAIF\Desktop\projects\bgv\bgv\events\models.py", line 17, in <module>
    from cms.models.fields import PlaceholderField
  File "C:\Users\SAIF\Anaconda3\envs\bgv\lib\site-packages\cms\models\__init__.py", line 3, in <module>
    from .permissionmodels import *  # nopyflakes
  File "C:\Users\SAIF\Anaconda3\envs\bgv\lib\site-packages\cms\models\permissionmodels.py", line 19, in <module>
    User = apps.get_registered_model(user_app_name, user_model_name)
  File "C:\Users\SAIF\Anaconda3\envs\bgv\lib\site-packages\django\apps\registry.py", line 273, in get_registered_model
    raise LookupError(
LookupError: Model 'accounts.CustomUser' not registered.

解决方案

1. 打破循环依赖

报错核心是循环导入导致用户模型未完成注册时,Django-CMS提前尝试加载用户模型:

  • Organization模型直接引用了EventVenue,而EventVenue导入了Django-CMS的PlaceholderField,触发CMS权限模块加载,此时accounts应用的模型还未完全注册。
  • 修改Organization中的外键为字符串引用,避免直接导入EventVenue:
# 原代码
responsible_for_room = models.ForeignKey(EventVenue, ...)
# 修改后
responsible_for_room = models.ForeignKey('events.EventVenue', on_delete=models.SET_NULL, blank=True, null=True, related_name='responsible_for_room')

2. 确认accounts应用加载优先级

已在INSTALLED_APPS中将accounts放在cms之前,此配置正确,需确保accounts应用的models.py没有依赖其他未提前加载的应用。

3. 验证自定义用户模型配置

  • 确认AUTH_USER_MODEL = 'accounts.CustomUser'无拼写错误,且放置在settings.py的正确位置(建议放在INSTALLED_APPS之前或附近)。
  • 确认CustomUserManager的实现无异常,确保用户模型能正常初始化。

4. 重置迁移并重启服务

  • 清理旧迁移(保留accounts和events应用的初始迁移文件),重新生成并执行迁移:
python manage.py makemigrations
python manage.py migrate
  • 重启Django开发服务器,确保所有模型完成注册。

内容的提问来源于stack exchange,提问作者Zarrai Saif

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 06:25:19