我想用 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 模型,希望可以帮助回答这个问题。
我能做些什么?