我的目标是在 Django 1.5 中创建自定义用户模型
# myapp.models.py
from django.contrib.auth.models import AbstractBaseUser
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
db_index=True,
)
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
company = models.ForeignKey('Company')
...
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['company']
由于公司字段(models.ForeignKey('Company')(python manage.py createsuperuser),我无法创建超级用户。我的问题:
如何在没有公司的情况下为我的应用程序创建超级用户。我试图制作自定义 MyUserManager 没有任何成功:
class MyUserManager(BaseUserManager):
...
def create_superuser(self, email, company=None, password):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(
email,
password=password,
)
user.save(using=self._db)
return user
还是我必须为这个用户创建一个假公司?谢谢