Python 无法在自定义超级用户Django中输入用户名
我想为Django创建一个自定义身份验证。还创建了自定义类和管理器。我无法使用--username属性运行命令python manage.py createsuperuser。它说没有这样的参数 如果没有用户名,则会给出null username的值错误。请帮忙 这是我的模特Python 无法在自定义超级用户Django中输入用户名,python,django,django-models,django-authentication,Python,Django,Django Models,Django Authentication,我想为Django创建一个自定义身份验证。还创建了自定义类和管理器。我无法使用--username属性运行命令python manage.py createsuperuser。它说没有这样的参数 如果没有用户名,则会给出null username的值错误。请帮忙 这是我的模特 from django.db import models from django.contrib.auth.models import AbstractBaseUser from django.contrib.auth.m
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager
class AccountManager(BaseUserManager):
def create_user(self, email, password=None, **kwargs):
if not email:
raise ValueError('Users must have a valid email address')
if not kwargs.get('username'):
raise ValueError('Users must have a valid username')
account = self.model(
email=self.normalize_email(email), username=kwargs.get('username')
)
account.set_password(password)
account.save()
return account
def create_superuser(self, email, password, **kwargs):
account = self.create_user(email, password, **kwargs)
account.is_admin = True
account.save()
return account
class Account(AbstractBaseUser):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True)
first_name = models.CharField(max_length=40, blank=True)
last_name = models.CharField(max_length=40, blank=True)
tagline = models.CharField(max_length=150, blank=True)
is_admin = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = AccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FILEDS = ['username']
def __unicode__(self):
return self.email
def get_full_name(self):
return ''.join([self.first_name, self.last_name])
def get_short_name(self):
return self.first_name
也许值得一看编写您自己的管理命令来处理自定义用户创建。您能否提供一个有用的链接。。?我是否真的需要编写一个自定义管理命令@KineticStackI不确定它是否100%必要,但它在未来可能会有所帮助,这取决于您如何使用它: