Django 3.2 LookupError:模型未注册

问题描述

在尝试将现有项目升级到 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,**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,**extra_fields)

apps/user_accounts/apps.py

from django.apps import AppConfig


class UserAccountsConfig(AppConfig):
    name = 'user_accounts'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)