经理不可用; “ auth.User”已交换为“ users.User”

时间:2019-10-09 18:25:59

标签: python django

1当我希望注册错误出现时我的代码有问题,管理器不可用; 'auth.User'已被替换为'users.User',我尝试其他问题的解决方案,与Manager不可用相同;我已经将'auth.User'换成'members.customer',但他们都要求替换User = User = get_user_model(),但是我在代码中没有使用任何User或我不知道在哪里使用过。 django,python,js等中的新功能,所以如果我的问题很愚蠢,请原谅我。

错误:

  / accounts / signup /管理器中的

AttributeError不可用;已将“ auth.User”替换为“ users.User”

Setting.Py

AUTH_USER_MODEL = 'users.User'

LOGOUT_REDIRECT_URL = '/'
LOGIN_REDIRECT_URL = '/'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'users.backends.EmailBackend',
)

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
    'avatar',
    'homepage',
    'products',`enter code here`
    #user part
    'users',
]

user / models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth import get_user_model

class User(AbstractUser):
    nickname = models.CharField(max_length = 50, blank = True)

    class Meta(AbstractUser.Meta):
        pass

accounts / models.py

from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

from django.conf import settings
User= settings.AUTH_USER_MODEL

class Profile(models.Model):
    """
    Define model for user profile with one-to-one relationship with User table.
    """
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    phone_number = models.CharField(max_length=20, blank=True)
    #country = CountryField(blank=True)
    about = models.TextField(max_length=500, blank=True)

# Define signals to update user profile whenever we create/update User model.       
@rece
iver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    """
    Create Profile object whenever new User object is created.
    """
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    """
    Update Profile object whenever new User object is updated.
    """
    instance.profile.save()

2 个答案:

答案 0 :(得分:0)

AUTH_USER_MODEL = 'users.User'

INSTALLED_APPS = [
    ...,
    'users',
]
  

user / models.py:

这应该是users

删除:

from django.contrib.auth.models import User

来自accounts/models.py

答案 1 :(得分:0)

只需尝试在 settings.py 文件的末尾添加此 AUTH_USER_MODEL = 'users.User'...