懒惰参考:不提供模型用户? [英] lazy reference: doesn't provide model user?

查看:59
本文介绍了懒惰参考:不提供模型用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用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("\n".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后端和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)

下面是我的模型。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 gave me an idea which worked.

我做了以下事情:


  1. 执行所有操作以放置自定义的 User 模型。在 settings.py 中设置 AUTH_USER_MODEL 并更新 django.contrib.auth.models的任何使用用户和自定义用户模型。

  2. 运行 python manage.py makemigrations

  3. 撤消步骤1

  4. 运行 python manage.py migration

  5. 重做步骤1

  1. Do everything to put your custom User model into place. Set AUTH_USER_MODEL in settings.py and update any uses of django.contrib.auth.models.User with your custom user model.
  2. Run python manage.py makemigrations
  3. Undo step 1
  4. Run python manage.py migrate
  5. Redo step 1

这篇关于懒惰参考:不提供模型用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆