'Is it possible to create a superuser with different inputs than a simple user in Django Rest Framework?

I am beginning a new project with Django Rest Framework, and I have a specifical need on the creation of a user: to create an account, you need to give information such as your birthdate, the birthdate of a friend, and several other info.

But it would NOT be relevant for a superuser to give such information, that's why I am looking for a way to require different info for user and for superuser. Do you know if it's possible ?

In the file models.py, I created 2 different classes :

  • class UserProfile(AbstractBaseUser, PermissionsMixin)
  • class SuperUserProfile(AbstractBaseUser, PermissionsMixin)

These two classes require different info to create an account.

In addition, I created a class to manage user profiles :

class UserProfileManager(BaseUserManager): """Manager for user profiles"""

def create_user(self, name1, firstName1, email1, name2, firstName2, birthDate2, password=None):
    """Create a new user profile"""

    email1 = self.normalize_email(emailParrain)
    user = self.model(emailParrain=emailParrain,
    name1=name1,
    firstName1=firstName1,
    name2=name2,
    firstNameUser=firstNameUser,
    birthDateUser=birthDateUser)  

    user.set_password(password)     
    user.save(using=self._db)

    return user

def create_superuser(self, email, name, password):
    """Create and save a new superuser with given details"""
    user = self.create_user(email,password)

    user.is_superuser = True
    user.is_staff = True
    user.save(using=self._db)

    return user

But when I do this, I cannot create a superuser with only the info sub-mentionned (email, name, password).



Solution 1:[1]

you just need to add the required fields and specifying the Manager in users models

class User (AbstractBaseUser, PermissionsMixin):

    user_name = models.CharField(max_length=150, unique=True)
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    ...

    objects = UserProfileManager()

    USERNAME_FIELD = 'user_name'
    #the user_name and first_name are required 
    REQUIRED_FIELDS = ['user_name', 'first_name']

    def __str__(self):
        return self.user_name

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oussama Tachi