Ankush paul
Ankush paul

Reputation: 257

TypeError: create_superuser() missing 1 required positional argument: 'profile_picture'

I get the following error after adding the profile_picture field:

TypeError: create_superuser() missing 1 required positional argument: 'profile_picture'

This profile_picture field is an "ImageField" set as "Null = True".

I have tried the following: def create_user(...., profile_picture=None, ....). It didn't work.

and the error occurs only in command prompt when i create superuser from there.

Here is my models.py

from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser


class UserManager(BaseUserManager):
    def create_user(self, email, full_name, profile_picture=None, gender=None, 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.gender = gender
        user.admin = is_admin
        user.profile_picture = profile_picture
        user.staff = is_staff
        user.active = is_active
        user.save(using=self._db)
        return user

    def create_staffuser(self, email, profile_picture, gender, full_name, password=None):
        user = self.create_user(
            email,
            full_name,
            profile_picture,
            gender,
            password=password,
            is_staff=True,
        )
        return user

    def create_superuser(self, email, profile_picture, gender, full_name, password=None):
        user = self.create_user(
            email,
            full_name,
            profile_picture,
            gender,
            password=password,
            is_staff=True,
            is_admin=True,
        )
        return user


class User(AbstractBaseUser):
    username = models.CharField(max_length=255)
    full_name = models.CharField(max_length=255)
    email = models.EmailField(max_length=255, unique=True,)
    profile_picture = models.ImageField(upload_to='user_data/profile_picture', null=True, blank=True)
    gender = models.CharField(max_length=255, blank=True, default='rather_not_say')
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)  # a admin user; non super-user
    admin = models.BooleanField(default=False)  # a superuser
    # notice the absence of a "Password field", that's built in.

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['full_name', 'gender']  # Email & Password are required by default.

    objects = UserManager()

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
         # The user is identified by their email address
         return self.email

    def __str__(self):              # __unicode__ on Python 2
         return self.email

    @staticmethod
    def has_perm(perm, obj=None):
         # "Does the user have a specific permission?"
         # Simplest possible answer: Yes, always
        return True

    @staticmethod
    def has_module_perms(app_label):
         # "Does the user have permissions to view the app `app_label`?"
         # Simplest possible answer: Yes, always
         return True

    @property
    def is_staff(self):
         # "Is the user a member of staff?"
         return self.staff

    @property
    def is_admin(self):
         # "Is the user a admin member?"
         return self.admin

    @property
    def is_active(self):
         # "Is the user active?"
         return self.active

Upvotes: 20

Views: 41028

Answers (10)

Lakhan Baheti
Lakhan Baheti

Reputation: 86

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

Upvotes: 1

Oreofreakshake
Oreofreakshake

Reputation: 66

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!

Upvotes: 2

Ali Cirik
Ali Cirik

Reputation: 1572

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

Upvotes: 26

INVESTORS
INVESTORS

Reputation: 41

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

Upvotes: 4

James-Epic Beats
James-Epic Beats

Reputation: 11

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

Upvotes: 1

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

Upvotes: 4

Gal Silberman
Gal Silberman

Reputation: 3904

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!

Upvotes: 19

Harshavardhan teja
Harshavardhan teja

Reputation: 37

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='[email protected]',
                          is_staff=True,
                          is_superuser=True,
                          phone='987654321',
                          gender='Male'
        
                        )
        user.set_password('anypassword')
        user.save()
    
    

    dependencies=[

    ]

    operations=[
            migrations.RunPython(seed_data),
        ]

Upvotes: 1

Harshavardhan teja
Harshavardhan teja

Reputation: 37

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

Upvotes: 1

pavan vamsi
pavan vamsi

Reputation: 49

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

Upvotes: 5

Related Questions