Django LookupError:模型未注册AUTH_USER_Model

2024-04-25 18:01:08 发布

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

django1.8和python3.4

我在我的应用程序中使用了一个名为UploaderClient的自定义用户模型authenticateclients。当我运行checkmakemigrationsmigrate时,我得到一个LookupError: Model 'authenticateclients.UploaderClient' not registered.错误。请帮忙。在

在设置.py我把AUTH_USER_MODEL定义为authenticateclients.UploaderClient

验证客户端/模型.py

    from django.db import models
    from django.contrib.auth.models import AbstractBaseUser, BaseUserManager

    # Create your models here.

   class UploaderClientManager(BaseUserManager):
       def create_user(self, accountname, password=None, **kwargs):
           if not accountname:
               raise ValueError('Users must have a valid accountname.')

           if not kwargs.get('email'):
               raise ValueError('Users must have a valid email.')

           if not kwargs.get('company_name'):
               raise ValueError('Users must have a valid company name.')

           account = self.model(
               accountname=self.normalize_accountname(accountname),email=kwargs.get('email'), company_name=kwargs.get('company_name')
           )

           account.set_password(password)
           account.save()

           return account

       def create_superuser(self, accountname, password, **kwargs):
           account = self.create_user(accountname, password, **kwargs)

           account.is_admin = True
           account.save()

           return account


   class UploaderClient(AbstractBaseUser):
       email = models.EmailField()
       accountname = models.CharField(max_length=100, unique=True)
       company_name = models.CharField(max_length=100)
       vuforiadb_name = models.CharField(max_length=100, blank=True)

       is_admin = models.BooleanField(default=False)

       created_at = models.DateTimeField(auto_now_add=True)
       updated_at = models.DateTimeField(auto_now=True)

       objects = UploaderClientManager()

       USERNAME_FIELD = 'accountname'
       REQUIRED_FIELDS = ['email','company_name']

       def __unicode__(self):
           return self.accountname

       def get_company_name(self):
           return self.company_name

       def get_vuforiadb_name(self):
           return self.vuforiadb_name

设置.py

^{pr2}$

当我运行checkmakemigrationsmigrate时,我得到

/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
  return f(*args, **kwds)

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/django/core/management/__init__.py", line 328, in execute
    django.setup()
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/cms/models/__init__.py", line 4, in <module>
    from .permissionmodels import *  # nopyflakes
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/cms/models/permissionmodels.py", line 29, in <module>
    User = apps.get_registered_model(user_app_name, user_model_name)
  File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/django/apps/registry.py", line 266, in get_registered_model
    "Model '%s.%s' not registered." % (app_label, model_name))
LookupError: Model 'authenticateclients.UploaderClient' not registered.

所以我所做的就是评论这句台词

AUTH_USER_MODEL = 'authenticateclients.UploaderClient'

然后运行makemigrationsmigrate。 迁移被应用。 然后在取消对上述行的注释并尝试check或{}或{}时,我仍然得到相同的错误。在

请帮助解决错误。 如果无法修复,我是否可以通过注释掉行来继续我的项目,或者如果我省略了行,身份验证将无法工作。。在


Tags: djangonameinpyselfhomemodelslib
3条回答

这个问题是LookupError: Model '' not registered.的第一个结果,所以我在这里添加:

对于那些在使用sqlite3db的开发环境中安装一些应用程序的用户,您可以简单地删除默认的project.db文件(如果您没有任何东西可以丢失的话)。在

查看错误堆栈跟踪,获取权限似乎是个问题:

File "/home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/cms/models/permissionmodels.py", line 29, in User = apps.get_registered_model(user_app_name, user_model_name)

django docsCustomizing authentication in Django解释说:

If you don’t include the PermissionsMixin, you must ensure you don’t invoke the permissions methods on ModelBackend. ModelBackend assumes that certain fields are available on your user model. If your User model doesn’t provide those fields, you will receive database errors when you check permissions.

然后,对于您的场景,避免错误的一个简单方法似乎是从PermissionsMixin继承。在

This is an abstract model you can include in the class hierarchy for your User model, giving you all the methods and database fields necessary to support Django’s permission model.

对于您的代码:

class UploaderClient(AbstractBaseUser, PermissionsMixin):
    ...

对于这个具体问题,@danihp已经指出了部分问题。你需要把应用程序与你的自定义模型前“cms”在你安装的应用程序。在

INSTALLED_APPS = {
    'your_custom_app',
    '...',
    'cms',
}

相关问题 更多 >