The Python Oracle

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

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

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping

--

Chapters
00:00 Question
02:21 Accepted answer (Score 13)
03:22 Answer 2 (Score 16)
03:43 Answer 3 (Score 4)
04:08 Answer 4 (Score 3)
04:55 Thank you

--

Full question
https://stackoverflow.com/questions/4947...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #django #djangocustomuser #djangocustommanager

#avk47



ANSWER 1

Score 25


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



ACCEPTED ANSWER

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




ANSWER 3

Score 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




ANSWER 4

Score 3


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