无法在Django 1.5中使用自定义用户模型创建超级用户

时间:2023-01-11 19:22:06


my goal is to create a custom user model in Django 1.5

我的目标是在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']

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:

由于公司字段(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

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

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

2 个解决方案

#1


4  

There are three ways for your in this case

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

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

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

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

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

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

3)保持型号代码不变。为名为'Superusercompany'的超级用户添加伪comapny,在create_superuser方法中设置它。

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

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

#2


2  

Thanks to your feedback here is the solution I made: A custom MyUserManager where I created a default company

感谢您的反馈,这里是我提出的解决方案:我创建默认公司的自定义MyUserManager

    def create_superuser(self, email, password, company=None):
        """
        Creates and saves a superuser with the given email and password.
        """

        if not company:
            company = Company(
                name="...",
                address="...",
                code="...",
                city="..."
            )
            company.save()

        user = self.create_user(
            email,
            password=password,
            company=company
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

#1


4  

There are three ways for your in this case

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

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

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

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

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

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

3)保持型号代码不变。为名为'Superusercompany'的超级用户添加伪comapny,在create_superuser方法中设置它。

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

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

#2


2  

Thanks to your feedback here is the solution I made: A custom MyUserManager where I created a default company

感谢您的反馈,这里是我提出的解决方案:我创建默认公司的自定义MyUserManager

    def create_superuser(self, email, password, company=None):
        """
        Creates and saves a superuser with the given email and password.
        """

        if not company:
            company = Company(
                name="...",
                address="...",
                code="...",
                city="..."
            )
            company.save()

        user = self.create_user(
            email,
            password=password,
            company=company
        )
        user.is_admin = True
        user.save(using=self._db)
        return user