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!
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',]
- Django ALLOWED_HOSTS with ELB HealthCheck
- Accessing django project in LAN systems
- How to make follower-following system with django model
- Django's get_current_language always returns "en"
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
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 π
- Django database synchronization for an offline usage
- Django's get_current_language always returns "en"
- Create a canonical "parent" product in Django Oscar programmatically
- In python django how do you print out an object's introspection? The list of all public methods of that object (variable and/or functions)?
- Sending a message to a single user using django-channels
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
- Celery β No module named five
- Django database synchronization for an offline usage
- How does this Man-In-The-Middle attack work?
- Django unable to find MySQLdb python module
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),
]
- Django's get_current_language always returns "en"
- Have a url that accepts all characters
- Django widget override template
- How does this Man-In-The-Middle attack work?
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!
- Django unable to find MySQLdb python module
- How do I simulate connection errors and request timeouts in python unit tests
- How can I disable a model field in a django form
- In python django how do you print out an object's introspection? The list of all public methods of that object (variable and/or functions)?
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):