Django 3.2 LookupError:模型未注册

2024-05-19 17:07:53 发布

您现在位置:Python中文网/ 问答频道 /正文

在尝试将现有项目升级到Django 3.2(从2.2)时,我遇到以下错误:

...
    from .permissionmodels import *  # nopyflakes
  File "/home/user/miniconda3/envs/app-web-py38-test/lib/python3.8/site-packages/cms/models/
permissionmodels.py", line 19, in <module>                                                            
    User = apps.get_registered_model(user_app_name, user_model_name)
  File "/home/user/miniconda3/envs/app-web-py38-test/lib/python3.8/site-packages/django/apps
/registry.py", line 273, in get_registered_model                                                      
    raise LookupError(
LookupError: Model 'user_accounts.CustomUser' not registered.

自定义模型位于apps/user_accounts/models.py中,如下所示

from django.db import models

from django.contrib.auth.models import AbstractUser
from django.db import models
from .managers import CustomUserManager

class CustomUser(AbstractUser, PermissionsMixin):
    username = None
    email = models.EmailField(('email address'), unique=True)
    # add additional fields in here
    profile_picture = models.ImageField(upload_to='profile/', blank=True)

    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email

安装的应用程序如下所示

INSTALLED_APPS = [
    # this app is at the top because it defines a custom user model for the whole project
    'apps.user_accounts',
    
    'djangocms_admin_style',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    #'django.contrib.contenttypes.models.ContentType',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sitemaps',

    #after project creation#

    #Django CMS stuff
    'django.contrib.sites',
    'cms',
    'menus',
    'treebeard',
    'sekizai',
    'filer',
    'easy_thumbnails',
    'mptt',
    'djangocms_link',
    'djangocms_file',
    'djangocms_picture',
    'djangocms_video',
    'djangocms_googlemap',
    'djangocms_snippet',
    'djangocms_style',
    'djangocms_column',
    'djangocms_text_ckeditor',
    'djangocms_comments',
    'bootstrap4',

    #Aldryn News Blog
    'aldryn_apphooks_config',
    'aldryn_categories',
    'aldryn_common',
    'aldryn_newsblog',
    #'aldryn_newsblog.apps.AldrynNewsBlog',
    'aldryn_people',
    'aldryn_translation_tools',
    'absolute',
    'aldryn_forms',
    'aldryn_forms.contrib.email_notifications',
    'captcha',
    'emailit',
    'parler',
    'sortedm2m',
    'taggit',
    # this AWS S3 work (boto + django-storages)
    'storages',
    'widget_tweaks',
    # Django Analytical for various analytics, i.e., Yandex Metrica, etc.
    'analytical',
    'meta',
    

    #Custom Apps
    'apps.custom_page_extensions',
    'apps.custom_social_addon',
    #'apps.pages',
    'apps.video_uploader',

    #search
    #'haystack',

    #bower
    'djangobower',

]

AUTH_USER_MODEL = 'user_accounts.CustomUser'

任何关于修复或故障排除的帮助都将不胜感激

编辑:

apps/user_accounts/managers.py

from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _


class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError(_('The Email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email, password, **extra_fields)

apps/user_accounts/apps.py

from django.apps import AppConfig


class UserAccountsConfig(AppConfig):
    name = 'user_accounts'

Tags: appsdjangofromimporttruefieldsismodels