djangodjango-modelsdjango-custom-user

Creating a superuser in custom user model


I have a custom user model as follows. When i try to create a superuser, it asks me organisation (organisation_id) which i don't want to give for superusers(because it makes sense only if the users give the organisation, as i need create a superuser just to view database and also superusers don't fall under any organisation). So what should i do to rectify this problem.

#models.py

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
# Create your models here.

class Organisation(models.Model):
    organisation_name =  models.CharField(max_length = 256)
    contact_no = models.IntegerField()
    email = models.EmailField()

    class Meta():
        unique_together = ['organisation_name','email']

    def  __str__(self):
        return self.organisation_name

class MyUserManager(BaseUserManager):
    def create_user(self, username, organisation, email, password):
        email = self.normalize_email(email)
        user = self.model(username=username, email=email, organisation=organisation)
        user.set_password(password)
        user.save(using=self.db)
        return user
    def create_superuser(self, username, email, password, organisation):
        user = self.create_user(username=username, email=email, password=password,organisation=organisation)
        user.is_superuser = True
        user.is_staff = True
        user.save()
        return user

class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=256,unique=True)
    email = models.EmailField(max_length=256)
    organisation = models.ForeignKey(Organisation,on_delete=models.CASCADE,null=True)


    USERNAME_FIELD = "username"
    REQUIRED_FIELDS = ['email','organisation']
    objects = MyUserManager()

    def __str__(self):
        return self.username

#views.py

class Create_Accounts(CreateView):
    model = models.MyUser
    fields=('__all__')
    template_name = 'accounts/create_account.html'
    success_url = reverse_lazy("accounts:inner_index")

#settings.py

AUTH_USER_MODEL = 'accounts.MyUser'

Solution

  • It is either required or not, there are no conditions model related. Meaning that if you set organisation inside REQUIRED_FIELDS then it will ask for it.

    Although, you can set conditions inside your manager. In this case just don't use create_user inside create_superuser.

    class MyUserManager(BaseUserManager):
        def create_user(self, username, organisation, email, password):
            if not organisation:
                raise ValueError("Users must have an organization")
            
            user = self.model(
                email = self.normalize_email(email),
                username=username,
                organisation=organisation
            )
            user.set_password(password)
            user.save(using=self.db)
            return user
        
        def create_superuser(self, username, email, password):
            user = self.model(
                email = self.normalize_email(email),
                username=username
            )
            user.set_password(password)
            user.is_staff=True
            user.save(using=self.db)
            return user
    

    And, remove organisation from MyUser REQUIRED_FIELDS. Despite that, there are still a few problems with your model. Namely, missing has_perm, has_module_perms functions and an is_staff field. Otherwise you will not be able to login into the admin page:

    class MyUser(AbstractBaseUser):
        ...
        is_staff = models.BooleanField(default=False)
    
    
        REQUIRED_FIELDS = ['email']
        ...
        
        def has_perm(self, perm, obj=None):
            return True
    
        def has_module_perms(self, app_label):
            return True