Django 1.8和Python 3.4
我在我的应用中使用名为UploaderClient
的自定义用户模型authenticateclients
。当我运行check
或makemigrations
或migrate
时,出现LookupError: Model 'authenticateclients.UploaderClient' not registered.
错误。请帮忙。
在settings.py中,我将AUTH_USER_MODEL
定义为authenticateclients.UploaderClient
authenticateclients / models.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
settings.py
"""
Django settings for ARPixelSite project.
Generated by 'django-admin startproject' using Django 1.8.6.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# -*- coding: utf-8 -*-
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
gettext = lambda s: s
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@-$h5gh5%s$70hd=ii55it!+4@a*u8b(c8aqumqkx@*m8%v89l'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'djangocms_admin_style', # for the admin skin. You **must** add 'djangocms_admin_style' in the list **before** 'django.contrib.admin'.
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'cms', # django CMS itself
'treebeard', # utilities for implementing a tree
'menus', # helper for model independent hierarchical website navigation
#'south', # Only needed for Django < 1.7
'sekizai', # for javascript and css management
'djangocms_file',
'djangocms_flash',
'djangocms_googlemap',
'djangocms_inherit',
'djangocms_picture',
'djangocms_teaser',
'djangocms_video',
'djangocms_link',
'djangocms_snippet',
'rest_framework',
'clientupload',
'authenticateclients',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
)
ROOT_URLCONF = 'ARPixelSite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'django.core.context_processors.static',
'sekizai.context_processors.sekizai',
'cms.context_processors.cms_settings',
],
},
},
]
WSGI_APPLICATION = 'ARPixelSite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'ARPixelDB',
'USER': 'djangouser',
'PASSWORD': 'djangouser',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
)
}
"""
TEMPLATE_DIRS = (
# The docs say it should be absolute path: BASE_DIR is precisely one.
# Life is wonderful!
os.path.join(BASE_DIR, "templates"),
)
"""
CMS_TEMPLATES = (
('template_1.html', 'Template One'),
('template_2.html', 'Template Two'),
)
LANGUAGES = [
('en', 'English'),
]
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
SITE_ID = 1
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
CMS_PAGE_MEDIA_PATH = os.path.join(MEDIA_ROOT, "cms_page_media")
AUTH_USER_MODEL = 'authenticateclients.UploaderClient'
当我运行check
或makemigrations
或migrate
时,我得到了
/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'
并运行makemigrations
和migrate
。
迁移得到了应用。
然后在取消注释上述行并尝试check
或makemigrations
或migrate
时,我仍然遇到同样的错误。
请帮助解决错误。 如果无法修复,我是否可以通过注释掉该行来继续我的项目,或者如果我遗漏该行,认证将不起作用。
答案 0 :(得分:5)
对于这个特定问题,@ danihp已经指出了部分问题。您需要将应用程序与自定义模型放在&#34; cms&#34;在你的INSTALLED_APPS中。
INSTALLED_APPS = {
'your_custom_app',
'...',
'cms',
}
答案 1 :(得分:0)
查看错误堆栈跟踪似乎是获得权限的问题:
File&#34; /home/dip7777/Desktop/ARPixelEnv/lib/python3.4/site-packages/cms/models/permissionmodels.py" ;,第29行,在 User = apps.get_registered_model(user_app_name,user_model_name)
django docs Customizing authentication in Django解释说:
如果不包含PermissionsMixin,则必须确保不要在ModelBackend上调用权限方法。 ModelBackend假定您的用户模型上有某些字段可用。如果您的用户模型未提供这些字段,则在检查权限时将收到数据库错误。
然后,对于您的方案,似乎避免错误的一种简单方法是从PermissionsMixin继承。
这是一个抽象模型,可以包含在User模型的类层次结构中,为您提供支持Django权限模型所需的所有方法和数据库字段。
代码:
class UploaderClient(AbstractBaseUser, PermissionsMixin):
...
答案 2 :(得分:0)
这个问题是LookupError: Model '' not registered.
的第一个结果,所以我在这里添加:
对于那些在带有sqlite3 db的开发环境中安装某些应用程序的人,您可以简单地删除默认的project.db
文件(如果没有丢失的话)。