我希望代码是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模型,希望能帮助回答这个问题。
我该怎么办?
答案 0 :(得分:1)
您应该构建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您将看到您必须延长create_user和create_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
最后,您必须完成整个示例的运行才能正确连接,follow along with the full example