My API endpoint http://127.0.0.1:8000/auth/convert-token encounters issues when creating a user, due to my custom user model which includes additional fields. The error message received is:
Steps to Reproduce
Attempt to use the convert-token endpoint to create a user.
The request fails with the error related to missing name and password arguments.
Expected Behavior
The user should be created successfully, even with a custom user model that includes additional fields. The convert-token endpoint should handle these fields correctly.
Actual Behavior
The error indicates that UserManager.create_user() is missing required positional arguments name and password.
Custom User Model
Here’s a snippet of my custom User model and UserManager:
# manager.py
from django.contrib.auth.models import BaseUserManager
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.contrib.auth.models import Group
class UserManager(BaseUserManager):
def email_validator(self, email):
try:
validate_email(email)
except ValidationError:
raise ValueError(_("Please enter a valid email address"))
def create_user(self, email, username, name, password, **extra_fields):
if not email:
raise ValueError(_("Email is required"))
email = self.normalize_email(email)
self.email_validator(email)
if not name:
raise ValueError(_("Name is required"))
if not username:
raise ValueError(_("Username is required"))
user = self.model(email=email, name=name, username=username, **extra_fields)
user.set_password(password)
user.save(using=self._db)
others_group = Group.objects.get(name='others')
user.groups.add(others_group)
return user
def create_superuser(self, email, name, username, password, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
extra_fields.setdefault("is_active", True)
extra_fields.setdefault("is_verified", True)
if extra_fields.get("is_staff") is not True:
raise ValueError(_("Superuser must have is_staff=True."))
if extra_fields.get("is_superuser") is not True:
raise ValueError(_("Superuser must have is_superuser=True."))
user = self.create_user(email, name, username, password, **extra_fields)
user.save(using=self._db)
admin_group = Group.objects.get(name='admin')
user.groups.add(admin_group)
return user
Solution Needed
I need guidance on how to ensure that user creation works correctly with my custom user model through the convert-token endpoint. Specifically:
How to adjust the convert-token endpoint to accommodate additional fields in my custom user model.
Ensuring that UserManager.create_user() handles all required fields properly.
Description
My API endpoint
http://127.0.0.1:8000/auth/convert-token
encounters issues when creating a user, due to my custom user model which includes additional fields. The error message received is:Steps to Reproduce
convert-token
endpoint to create a user.name
andpassword
arguments.Expected Behavior
The user should be created successfully, even with a custom user model that includes additional fields. The
convert-token
endpoint should handle these fields correctly.Actual Behavior
The error indicates that
UserManager.create_user()
is missing required positional argumentsname
andpassword
.Custom User Model
Here’s a snippet of my custom
User model
andUserManager
:Solution Needed
I need guidance on how to ensure that user creation works correctly with my custom user model through the
convert-token
endpoint. Specifically:convert-token
endpoint to accommodate additional fields in my custom user model.UserManager.create_user()
handles all required fields properly.