简体   繁体   English

Django错误:“管理器”对象上的AttributeError没有属性“ create_user”

[英]Django error: AttributeError at 'Manager' object has no attribute 'create_user'

models.py: models.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 views.py

from projectname.app.models import appUser

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

settings.py settings.py

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

When I am trying to call a create-user method in view this exception appears. 当我尝试在视图中调用create-user方法时,会出现此异常。 What am I doing wrong? 我究竟做错了什么?

Your appUser is a subclass of user . 您的appUseruser的子类。 If user is not an abstract base class, then appUser will not inherit the manager, and you will need to redeclare it. 如果user不是抽象基类,则appUser将不会继承管理器,因此您需要重新声明它。

class appUser(user):
    objects = UserManager()

From 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. 因此,它们不会传递给子类。

Having your appUser model subclass user is complex, you might encounter other difficulties. appUser模型子类user很复杂,您可能会遇到其他困难。 I would avoid it if possible, or at least make user abstract. 如果可能的话,我会避免它,或者至少使user抽象化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'Manager' 对象在 django 1.7 中没有属性 'create_user' - 'Manager' object has no attribute 'create_user' in django 1.7 在Django自定义用户管理器中,create_user和_create_user()之间的区别是什么? - In Django custom user manager, difference between create_user and _create_user()? Django 创建超级用户错误:AttributeError: 'ProfileManager' 对象没有属性 'create_superuser' - Django creating super user error: AttributeError: 'ProfileManager' object has no attribute 'create_superuser' Django AttributeError: 'WSGIRequest' 对象没有属性 'user' - Django AttributeError: 'WSGIRequest' object has no attribute 'user' Django: AttributeError: 'User' object 没有属性 'get' - Django: AttributeError: 'User' object has no attribute 'get' AttributeError: 'Manager' 对象没有属性 - AttributeError: 'Manager' object has no attribute 创建超级用户时出现Django错误,AttributeError:'Manager'对象没有属性'get_by_natural_key' - Django error while creating superuser, AttributeError: 'Manager' object has no attribute 'get_by_natural_key' Django 2.0.3 AttributeError:'Manager'对象没有属性'objects' - Django 2.0.3 AttributeError: 'Manager' object has no attribute 'objects' Django AttributeError:“ Manager”对象没有属性“ select_for_update” - Django AttributeError: 'Manager' object has no attribute 'select_for_update' AttributeError: 'Manager' object has no attribute 'get_by_natural_key' 错误在Django? - AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM