gpt4 book ai didi

python - Django 错误 : AttributeError at 'Manager' object has no attribute 'create_user'

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:19 25 4
gpt4 key购买 nike

模型.py:

    from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.models import BaseUserManager

class UserManager(BaseUserManager):
# code
def create_user(self, uid, pass):
# code

class user(AbstractBaseUser, PermissionsMixin):
# code
objects = UserManager()
# code

class appUser(user):
# code

views.py

from projectname.app.models import appUser

def index_view(request):
# code
appUser.objects.create_user(uid_variable, pass_variable)
# code

settings.py

#code
INSTALLED_APPS = (
#code
'projectname.app',
)
AUTH_USER_MODEL = 'app.appUser'
#code

当我尝试在 View 中调用创建用户方法时出现此异常。我做错了什么?

最佳答案

您的appUseruser 的子类。如果user不是抽象基类,那么appUser就不会继承manager,需要重新声明。

class appUser(user):
objects = UserManager()

来自 the docs :

Managers defined on non-abstract base classes are not inherited by child classes. If you want to reuse a manager from a non-abstract base, redeclare it explicitly on the child class. These sorts of managers are likely to be fairly specific to the class they are defined on, so inheriting them can often lead to unexpected results (particularly as far as the default manager goes). Therefore, they aren’t passed onto child classes.

拥有 appUser 模型子类 user 很复杂,您可能会遇到其他困难。如果可能的话,我会避免它,或者至少使 user 抽象。

关于python - Django 错误 : AttributeError at 'Manager' object has no attribute 'create_user' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33957188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com