django 无法创建超级用户

django can't create superuser

我想用 Django 编写博客。默认用户模型不适合我的博客,我想编写自己的用户。

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

class MyUserManager(BaseUserManager):
    def create_superuser(self, email, username, password, created_at):
        user = self.create_user(email=email,
            username=username,
            password = password,
            created_at = created_at
            )
        user.is_admin = True
        user.save(using=self._db)
        return user

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name = 'email address',
        max_length = 255,
        unique = True,
        )

    username = models.CharField(
        max_length = 100,
        unique = True,
        db_index = True,
        )

    created_at = models.DateField()

    is_active = models.BooleanField(default = True)
    is_admin = models.BooleanField(default = False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.username

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

有错误

我按照教程说明进行操作。不知道为什么会出现这个问题

我是postMyUser模型,希望能帮忙解答一下。

我能做什么?

您应该构建 MyUserManager 的实例并使用所有位置参数调用其 create_superuser 方法。您收到 TypeError 是因为框架正在调用 create_superuser 而没有适当的位置参数。如果您在 运行 一项 manage.py 任务(例如 manage.py createsuperuser)时收到此错误,那是因为 manage.py 正在尝试调用 create_superuser 而未获得正确的号码的争论在一起。这些参数通过在 MyUser 对象中设置适当的字段来处理:

考虑 create_superuser 方法中的附加参数

USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', 'created_at']

您的代码还有其他严重问题。 来自 "Customizing Authentication" documentation you'll see that you must extend create_user and create_superuser 在这两种情况下,username_field 是第一个位置参数,而在 create_superuser 中,密码是第二个位置字段。我已修改您的代码以在 MyUserManager class.

中包含 create_user 方法
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models

class MyUserManager(BaseUserManager):
    def create_user(self, username, password, email, created_at):
        """
        Creates and saves a User with the given username, password, email, created_a
        birth and password.
        """
        if not username:
            raise ValueError('Users must have a username')
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            username=username,
            email=self.normalize_email(email),
            created_at=created_at,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

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

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name = 'email address',
        max_length = 255,
        unique = True,
        )

    username = models.CharField(
        max_length = 100,
        unique = True,
        db_index = True,
        )

    created_at = models.DateField()

    is_active = models.BooleanField(default = True)
    is_admin = models.BooleanField(default = False)

    objects = MyUserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email', 'created_at']

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.username

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

最后,您必须 运行 完成示例才能正确连接所有内容,follow along with the full example