Can't create super user with custom user model in Django 1.5
Asked Answered
T

2

10


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']

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 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

Tuyettv answered 13/4, 2013 at 13:13 Comment(3)
Why company is required?Herr
In my model, a user without a company can't exist. But there is an exception for the superuser. I got this error without REQUIRED_FIELDS : IntegrityError: app_myuser.company_id may not be NULLTuyettv
You can specify a default company for all.Herr
D
7

There are three ways for you in this case

1) Make relation to company Not required 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

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

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

Disafforest answered 13/4, 2013 at 14:59 Comment(0)
T
5

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

    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
Tuyettv answered 14/4, 2013 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.