[Fixed]-TypeError: create_superuser() missing 1 required positional argument: 'profile_picture'

17πŸ‘

βœ…

Well, you need to create the create_superuser function as well:

class UserManager(BaseUserManager):
    def create_user(self, email, full_name, profile_picture, password=None, is_admin=False, is_staff=False, is_active=True):
        if not email:
            raise ValueError("User must have an email")
        if not password:
            raise ValueError("User must have a password")
        if not full_name:
            raise ValueError("User must have a full name")

        user = self.model(
            email=self.normalize_email(email)
        )
        user.full_name = full_name
        user.set_password(password)  # change password to hash
        user.profile_picture = profile_picture
        user.admin = is_admin
        user.staff = is_staff
        user.active = is_active
        user.save(using=self._db)
        return user
        
    def create_superuser(self, email, full_name, profile_picture, password=None, **extra_fields):
        if not email:
            raise ValueError("User must have an email")
        if not password:
            raise ValueError("User must have a password")
        if not full_name:
            raise ValueError("User must have a full name")

        user = self.model(
            email=self.normalize_email(email)
        )
        user.full_name = full_name
        user.set_password(password)
        user.profile_picture = profile_picture
        user.admin = True
        user.staff = True
        user.active = True
        user.save(using=self._db)
        return user

Good Luck!

πŸ‘€Gal Silberman

23πŸ‘

You can add username to the REQUIRED_FIELDS. After that python manage.py createsuperuser asks for username field and it works.

REQUIRED_FIELDS = ['full_name', 'gender', 'username',]
πŸ‘€Ali Cirik

4πŸ‘

I had the same problem, it turned out that in the list named REQUIRED_FIELDS was misnamed.
That list tells the django framework to ask for name as well during the creation. Because it is not asking and you’ve made it necessary.
I hope it helps, best of luck

πŸ‘€pavan vamsi

4πŸ‘

i solved this problem with some changes

my old code

class User(AbstractUser):
    username = None
    name = models.CharField(max_length=200, null=True)
    email = models.EmailField(unique=True, null=True)
    bio = models.TextField(null=True)

    avatar = models.ImageField(null=True, default="avatar.svg")
 
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

and i edited username field to email field

class User(AbstractUser):
    username = models.EmailField(unique=True, null=True)
    name = models.CharField(max_length=200, null=True)
    bio = models.TextField(null=True)

    avatar = models.ImageField(null=True, default="avatar.svg")
 
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []

Conculation
username = None => username = models.EmailField(unique=True, null=True)

Happy coding πŸ™‚

πŸ‘€INVESTORS

2πŸ‘

You need to add "profile_picture" to "REQUIRED_FIELDS" in the class "User(AbstractBaseUser)" in "models.py":

# "models.py"
class User(AbstractBaseUser):
    # ...                           # Here
    REQUIRED_FIELDS = ['full_name', 'profile_picture', 'gender']
    # ...

1πŸ‘

Better to create a class in 0001.initial.py file of migrations. Define a class with all required fields for login and provide dependencies and operations blocks empty.That’s it

1πŸ‘

Make a 0001_initial.py file inside the migrations folder and follow up the below code it will work…

from django.db import migrations
from api.user.models import CustomUser

class Migration(migrations.Migration):
    
    def seed_data(apps, schema_editor):
        user = CustomUser(name='name',
                          email='mail@gmail.com',
                          is_staff=True,
                          is_superuser=True,
                          phone='987654321',
                          gender='Male'
        
                        )
        user.set_password('anypassword')
        user.save()
    
    

    dependencies=[

    ]

    operations=[
            migrations.RunPython(seed_data),
        ]

1πŸ‘

You need to add profile_picture to REQUIRED_FIELDS in the class User(AbstractBaseUser) in models.py:

1πŸ‘

class UserData(AbstractUser):
    UserDataid = models.AutoField(primary_key=True)
    _name = models.CharField(max_length=255)
    email = models.CharField(max_length=255, unique=True)
    password = models.CharField(max_length=255)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

i had a model like this throwing a similar error, just needed to add "username" to REQUIRED_FIELDS and was good to go!

1πŸ‘

Actually you are making profile_picture parameter optional in create_user function:

 def create_user(self, email, full_name, profile_picture=None, gender=None, password=None, is_admin=False, is_staff=False, is_active=True):

And for create_superuser function your code is :

 def create_superuser(self, email, profile_picture, gender, full_name, password=None):

SOLUTION :

 def create_superuser(self, full_name, email, password, profile_picture=None, gender=None, is_admin=True, is_staff=True, is_active=True):

And one shortcut is there to handle these many parameters:

def create_superuser(self, email, password, **extra_fields):
πŸ‘€Lakhan Baheti

Leave a comment