I created a custom serializer that extends dj_rest_auth's RegisterSerializer
, added the extra user_type
field, overrode the get_cleaned_data
method to include the user_type
in the cleaned data then configured the custom registration serializer used for registration.
Here's my code:
class CustomRegisterSerializer(RegisterSerializer):
user_type = serializers.ChoiceField(choices=[('seeker', 'Seeker'), ('recruiter', 'Recruiter')])
class Meta:
model = User
fields = "__all__"
def get_cleaned_data(self):
data = super().get_cleaned_data()
data['user_type'] = self.validated_data.get('user_type', '')
return data
class Profile(models.Model):
class Type(models.TextChoices):
seeker = "Seeker"
recruiter = "Recruiter"
base_type = Type.seeker
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_type = models.CharField(choices=Type.choices, default=base_type, max_length=20)
class Meta:
verbose_name = "Profile"
verbose_name_plural = "Profiles"
def __str__(self):
return self.user.username
class CustomRegisterView(RegisterView):
serializer_class = CustomRegisterSerializer