在Django 1.5中无法创建具有自定义用户模型的超级用户 [英] Can't create super user with custom user model in Django 1.5

查看:143
本文介绍了在Django 1.5中无法创建具有自定义用户模型的超级用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的目标是在Django 1.5中创建一个自定义用户模型


my goal is to create a custom user model in 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 creatinguperuser)创建一个超级用户
我的问题:

我如何为我创建一个超级用户没有公司的申请
我试图做ac自定义MyUserManager没有任何成功:

I can't create a super user because of the company field (models.ForeignKey('Company') (python manage.py createsuperuser). My question:
How can I create a super user for my application without a company. I tried to make a custom MyUserManager without any success:

class MyUserManager(BaseUserManager):
    ...

    def create_superuser(self, email, company=None, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.save(using=self._db)
        return user

或者我必须为此用户创建一个假公司?
谢谢

Or do I have to create a fake company for this user? Thank you

推荐答案

在这种情况下,有三种方法

There are three ways for your in this case

1)与comapany的关系不需要 company = models.ForeignKey('Company',null = True)

1) Make relation to comapany Not required company = models.ForeignKey('Company',null=True)

2)添加默认公司并将其作为默认值提供给外键字段 company = models.ForeignKey('Company',default = 1) #where 1 is id创建公司

2) Add default company and provide it as default value to foreign key field company = models.ForeignKey('Company',default=1) #where 1 is id of created company

3)保留模型代码。为超级用户添加假的comapny命名为例如超级用户公司
在create_superuser方法中设置。

3) Leave model code as is. Add fake comapny for superuser named for example 'Superusercompany' set it in create_superuser method.

UPD:根据你的评论3将是最好的解决方案,不要打破你的灌输逻辑。

UPD: according to your comment 3 would be the best solution not to break your bushiness logic.

这篇关于在Django 1.5中无法创建具有自定义用户模型的超级用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆