-2

我想用 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

它有一个错误。在此处输入图像描述

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

我发布了 MyUser 模型,希望可以帮助回答这个问题。

我能做些什么?

4

1 回答 1

2

您应该构造一个实例并使用所有位置参数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']

您的代码还有其他严重问题。从“自定义身份验证”文档中,您将看到在这两种情况下都必须扩展create_usercreate_superuser ,username_field 是第一个位置参数,而在 create_superuser 中,password 是第二个位置字段。我已修改您的代码以create_user在类中包含该方法MyUserManager

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

最后,您必须完成该示例的运行以正确连接所有内容,并按照完整示例进行操作

于 2015-05-19T16:04:37.657 回答