懒惰的参考:不提供模型用户? [英] lazy reference: doesn't provide model user?
问题描述
目前我使用的是 Django 1.11 和 Python 3.6.我正在尝试使用通过 LDAP 进行身份验证的新应用程序创建自定义用户模型,但收到以下错误消息.
Currently I'm using Django 1.11 and Python 3.6. I'm attempting to create a custom user model with a new application that authenticates with LDAP, but i'm greeted with the following error message.
raise ValueError("
".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.user', but app 'accounts' doesn't provide model 'user'.
Settings.py 安装的应用程序、身份验证后端和 Auth_User 模型:
Settings.py Installed Apps, Auth Backends, and Auth_User Model:
INSTALLED_APPS = [
'django_python3_ldap',
'django_extensions',
'django_filters',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
]
AUTHENTICATION_BACKENDS = (
'django_python3_ldap.auth.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
AUTH_USER_MODEL = "accounts.User"
Admin.py:
from django.contrib import admin
from django.conf import settings
from .models import User
# Register your models here.
admin.site.register(User)
下面是我的models.py:
Below is my models.py:
from __future__ import unicode_literals
from django.utils import timezone
from django.contrib.auth.models import (AbstractBaseUser,PermissionsMixin)
from django.db import models
from django.forms import ModelForm
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
username = models.CharField(max_length=25, unique=True)
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=140)
date_joined = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
facility = models.CharField(max_length=140)
jobdescription = models.CharField(max_length=140)
positiondescription = models.CharField(max_length=140)
USERNAME_FIELD = "username"
推荐答案
这个:
这是由您的设置引起的.AUTH_USER_MODEL 已更改为计算迁移时不存在的模型.
This is caused by your settings.AUTH_USER_MODEL having changed to a model that does not exist when migrations are being computed.
... 由 @AKX 在 他们在下面的回答 给了我一个可行的想法.
... mentioned by @AKX in their answer below gave me an idea which worked.
我做了以下事情:
- 尽一切努力将您的自定义
User
模型放置到位.在settings.py
中设置AUTH_USER_MODEL
并使用您的自定义用户模型更新django.contrib.auth.models.User
的任何使用. - 运行
python manage.py makemigrations
- 撤消第 1 步
- 运行
python manage.py migrate
- 重做第 1 步
- Do everything to put your custom
User
model into place. SetAUTH_USER_MODEL
insettings.py
and update any uses ofdjango.contrib.auth.models.User
with your custom user model. - Run
python manage.py makemigrations
- Undo step 1
- Run
python manage.py migrate
- Redo step 1
这篇关于懒惰的参考:不提供模型用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!