python - 经理不在; 'auth.User' 已替换为 'users.User'

标签 python django

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

错误:

AttributeError at /accounts/signup/ Manager isn't available; 'auth.User' has been swapped for '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()

最佳答案

AUTH_USER_MODEL = 'users.User'

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

user/models.py:

这应该是用户

删除:

from django.contrib.auth.models import User

来自accounts/models.py

关于python - 经理不在; 'auth.User' 已替换为 'users.User',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58310159/

相关文章:

python - 我如何先读取二进制 pickle 数据,然后再对其进行 unpickle?

django - 我的 Django 网址不接受破折号

python - ValueError at/posts/create/invalidliteral for int() with base 10 : 'create' in django Form

python - 多个 Postgres 模式的 Django 单元测试失败

django - "Need a valid file name!"xhtml2pdf 与 Django

python - 如何在Django项目中使用elasticsearch?

c# - XML-RPC C# 和 Python RPC 服务器

python - 向使用 PyYaml 生成的 YAML 添加注释

python - 访问 API 时在 Python 中收到请求错误 404

Python - 在 if 语句中使用字符串作为条件