I have being using djoser with django to handle register and login. I have no issue configuring the default register field (username, email, password and re_password).
But now I want to customize the register field and add gender
and hobby
. However, I keep getting the following error when I try to register another user with these additional fields that djoser
does not provide by default.
user = User.objects.create_user(**validated_data)
TypeError: UserManager.create_user() missing 2 required positional arguments: 'gender', 'hobby'
The error is coming from the function in models.py file for creating new user shown below:
class UserManager(BaseUserManager):
def create_user(self, username, email, gender, hobby, password=None, **extra_fields):
if username is None:
raise ValueError(_('Username is required'))
if email is None:
raise ValueError(_('Enter a functional email address'))
if gender is None:
raise ValueError(_('Provide your gender'))
if hobby is None:
raise ValueError(_('Enter hobby'))
user = self.model(email=self.normalize_email(email) **extra_fields)
user.username=username
user.gender=gender,
user.hobby=hobby
user.set_password(password)
user.save()
return user
def create_superuser(self,username, email, gender, hobby, password=None):
if password is None:
raise ValueError(_('Please enter your password'))
user = self.create_user(email=self.normalize_email(email))
user.username=username
user.gender=gender,
user.hobby=hobby
user.is_superuser = True
user.is_staff = True
user.save()
return user
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=25, unique=True, default='')
email = models.EmailField(max_length=255, unique=True)
gender = models.CharField(max_length=50, null=True, blank=True)
hooby = models.CharField(max_length=50, null=True, blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.username
serializers.py
class UserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
model = User
fields = (
'id',
'username', 'email',
'gender', 'hobby',
'password'
)
settings.py
djoser = {
'SERIALIZERS': {
'user_create': 'accounts.serializers.UserCreateSerializer',
'user': 'accounts.serializers.UserCreateSerializer',
'current_user': 'accounts.serializers.UserCreateSerializer',
'user_delete': 'djoser.serializers.UserDeleteSerializer',
}
}
I have gone through the code several times I still can't find why I'm getting the error
I had the same problem. added the field into the REQUIRED_FIELDS in the User model and problem solved. just add gender and hobby into the REQUIRED_FIELDS