python-3.xdjangodjango-modelsdjango-cms

Django-cms with custom user model: LookupError: Model 'accounts.CustomUser' not registered


Using Django 3.2/Django-cms 3.11.

I am encountering an issue when trying to add PlaceholderField from cms.models.fields in my events.models module to the EventVenue model.

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

the settings:

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'

the custom user class:

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

Here's the relevant part of the traceback:

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.

i have tried to change the app order in the INSTALLED_APPS and inherit from the PermissionsMixin in the custom user but didn't work

the Organization class in the accounts app:

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


Solution

  • After a few tests with adding AUTH_USER_MODEL in my current project, my conclusions are the following:

    So for this specific case/question, it would mean checking the accounts app for any cms imports. And also, for any other imports that could trigger cms imports, as for example the Organization - this could be removed, and the foreign key be noted as models.ForeignKey("organization_app.Organization",....) - as I don't know where Organization lives.

    This is not fully researched, but could help anyway. Fun fact: If you specify AUTH_USER_MODEL = "auth.User" everything runs ok.