简体   繁体   中英

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

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

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

When I am trying to call a create-user method in view this exception appears. What am I doing wrong?

Your appUser is a subclass of user . If user is not an abstract base class, then appUser will not inherit the manager, and you will need to redeclare it.

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. I would avoid it if possible, or at least make user abstract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM