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

当我执行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'

最佳答案

应该是objects = UserAccountManager()
https://docs.djangoproject.com/en/1.10/topics/db/managers/#custom-managers-and-model-inheritance

09-06 17:18