I get the following error after adding the profile_picture
field:
JavaScript
x
2
1
TypeError: create_superuser() missing 1 required positional argument: 'profile_picture'
2
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
JavaScript
1
105
105
1
from django.db import models
2
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
3
4
5
class UserManager(BaseUserManager):
6
def create_user(self, email, full_name, profile_picture=None, gender=None, password=None, is_admin=False, is_staff=False, is_active=True):
7
if not email:
8
raise ValueError("User must have an email")
9
if not password:
10
raise ValueError("User must have a password")
11
if not full_name:
12
raise ValueError("User must have a full name")
13
14
user = self.model(
15
email=self.normalize_email(email)
16
)
17
user.full_name = full_name
18
user.set_password(password) # change password to hash
19
# user.profile_picture = profile_picture
20
user.gender = gender
21
user.admin = is_admin
22
user.profile_picture = profile_picture
23
user.staff = is_staff
24
user.active = is_active
25
user.save(using=self._db)
26
return user
27
28
def create_staffuser(self, email, profile_picture, gender, full_name, password=None):
29
user = self.create_user(
30
email,
31
full_name,
32
profile_picture,
33
gender,
34
password=password,
35
is_staff=True,
36
)
37
return user
38
39
def create_superuser(self, email, profile_picture, gender, full_name, password=None):
40
user = self.create_user(
41
email,
42
full_name,
43
profile_picture,
44
gender,
45
password=password,
46
is_staff=True,
47
is_admin=True,
48
)
49
return user
50
51
52
class User(AbstractBaseUser):
53
username = models.CharField(max_length=255)
54
full_name = models.CharField(max_length=255)
55
email = models.EmailField(max_length=255, unique=True,)
56
profile_picture = models.ImageField(upload_to='user_data/profile_picture', null=True, blank=True)
57
gender = models.CharField(max_length=255, blank=True, default='rather_not_say')
58
active = models.BooleanField(default=True)
59
staff = models.BooleanField(default=False) # a admin user; non super-user
60
admin = models.BooleanField(default=False) # a superuser
61
# notice the absence of a "Password field", that's built in.
62
63
USERNAME_FIELD = 'email'
64
REQUIRED_FIELDS = ['full_name', 'gender'] # Email & Password are required by default.
65
66
objects = UserManager()
67
68
def get_full_name(self):
69
# The user is identified by their email address
70
return self.email
71
72
def get_short_name(self):
73
# The user is identified by their email address
74
return self.email
75
76
def __str__(self): # __unicode__ on Python 2
77
return self.email
78
79
@staticmethod
80
def has_perm(perm, obj=None):
81
# "Does the user have a specific permission?"
82
# Simplest possible answer: Yes, always
83
return True
84
85
@staticmethod
86
def has_module_perms(app_label):
87
# "Does the user have permissions to view the app `app_label`?"
88
# Simplest possible answer: Yes, always
89
return True
90
91
@property
92
def is_staff(self):
93
# "Is the user a member of staff?"
94
return self.staff
95
96
@property
97
def is_admin(self):
98
# "Is the user a admin member?"
99
return self.admin
100
101
@property
102
def is_active(self):
103
# "Is the user active?"
104
return self.active
105
Advertisement
Answer
Well, you need to create the create_superuser
function as well:
JavaScript
1
41
41
1
class UserManager(BaseUserManager):
2
def create_user(self, email, full_name, profile_picture, password=None, is_admin=False, is_staff=False, is_active=True):
3
if not email:
4
raise ValueError("User must have an email")
5
if not password:
6
raise ValueError("User must have a password")
7
if not full_name:
8
raise ValueError("User must have a full name")
9
10
user = self.model(
11
email=self.normalize_email(email)
12
)
13
user.full_name = full_name
14
user.set_password(password) # change password to hash
15
user.profile_picture = profile_picture
16
user.admin = is_admin
17
user.staff = is_staff
18
user.active = is_active
19
user.save(using=self._db)
20
return user
21
22
def create_superuser(self, email, full_name, profile_picture, password=None, **extra_fields):
23
if not email:
24
raise ValueError("User must have an email")
25
if not password:
26
raise ValueError("User must have a password")
27
if not full_name:
28
raise ValueError("User must have a full name")
29
30
user = self.model(
31
email=self.normalize_email(email)
32
)
33
user.full_name = full_name
34
user.set_password(password)
35
user.profile_picture = profile_picture
36
user.admin = True
37
user.staff = True
38
user.active = True
39
user.save(using=self._db)
40
return user
41
Good Luck!