'经理'对象没有属性' get_by_natural_key'

时间:2017-02-06 19:36:17

标签: django django-authentication

我知道有类似这样的问题,我尝试了解决这些问题的所有解决方案。这是我描述的问题;

当我执行createsuperuser时,我收到如下错误;

AttributeError: 'Manager' object has no attribute 'get_by_natural_key'

以下是我在实现中定义UserAccountManager和UserAccount的方法;

from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser


class UserAccountManager(BaseUserManager):
    def create_user(self, email, first_name, last_name, password=None):
        if not email:
            raise ValueError('Email must be set!')
        user = self.model(email=email, first_name=first_name, last_name=last_name)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, first_name, last_name, password):
        user = self.create_user(email, first_name, last_name, password)
        user.is_admin = True
        user.save(using=self._db)
        return user

    def get_by_natural_key(self, email_):
        return self.get(code_number=email_)


class UserAccount(AbstractBaseUser):
    email = models.EmailField(unique=True)
    first_name = models.CharField(max_length=128)
    last_name = models.CharField(max_length=128)
    is_active = models.BooleanField(default=True) # default=False when you are going to implement Activation Mail
    is_admin = models.BooleanField(default=False)

    objects = UserAccountManager

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name']

    def get_short_name(self):
        return self.email

    def get_full_name(self):
        return  self.email

    def has_perms(self, perm, ob=None):
        return True

    def has_module_perms(self, app_label):
        return True

    def natural_key(self):
        return self.email

    @property
    def is_staff(self):
        self.is_admin

我在UserAccount类中设置对象如下:objects = UserAccountManager

我做错了什么?

编辑(解决Iain Shelvington的解决方案): 在settings.py中,我有这一行;

AUTH_USER_MODEL = 'accounts.UserAccount'

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

您需要将设置AUTH_USER_MODEL更改为'your_app.UserAccount'

答案 2 :(得分:0)

将对象= UserAccountManager更改为对象= UserAccountManager()。