I'm using Djoser(https://github.com/sunscrapers/djoser) along with Django Rest Framework. I'm using the default /register/ endpoint provided by Djoser. Now, I want to pass additional information at the time of registering a user. Say for example the referral code (to see where the user came from). I think I need to implement this logic in create_user method of my UserManager class.
Here is my MyUserManager:
class MyUserManager(BaseUserManager):
def create_user(self, name, email, referrer_code="", password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
print "referrercode" + referrer_code
referrer = 0
if (referrer_code != ""):
try:
referrer = MyUser.objects.filter(referral_code=referrer_code).first().id
except:
referrer = 0
user = self.model(
name=name,
email=self.normalize_email(email),
referrer=referrer,
)
user.set_password(password)
user.save(using=self._db)
return user
Here, referrer_code
is what I want to pass as a part of the POST request.
Here is my MyUser model:
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
name = models.CharField(max_length=255, blank=True)
referrer = models.IntegerField(default=0)
referral_code = models.CharField(max_length=10, blank=True)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', ]
This is what it says on the djoser documentation page:
Use this endpoint to register new user. Your user model manager should implement create_user method and have USERNAME_FIELD and REQUIRED_FIELDS fields.
However, in my case, referrer_code is not a field of the user that is being registered, it is a field of the user who referred the new one. So I don't think including referral_code as a required field would help.
Open auth/urls.py and add register endpoint: we should send a POST request to API for checking register endpoint. You must add username, password, password2, email, first_name, last_name to body. If fields passed validations, new user detail will be returning.
Djoser is a simple authentication library for Django. It is used to generate tokens for authentication; this generated token is generated by taking three fields: username, email and password. It only works on POST request, but you can add its frontend.
May or may not be helpful and this is old, but it's still unanswered so here goes.
Came across a similar issue where I was using a custom User model (extending AbstractBaseUser), and despite anything I did on the serializer, the DRF would only show the default fields - I think name, email, and password.
I'd gone through the docs and done everything specified, including specifying the serializer in settings.py
as they do in the example in the docs:
'user': 'myapp.serializers.SpecialUserSerializer'
, but still no dice.
Turns out that you need a serializer for each endpoint. I'd expected that you'd define a User
serializer, and the registration endpoint would grab from that. Instead, you need a user_registration
endpoint that defines just the stuff necessary for registration. These are all the defaults, each of which you'd have to override as applicable:
{
'activation': 'djoser.serializers.ActivationSerializer',
'login': 'djoser.serializers.LoginSerializer',
'password_reset': 'djoser.serializers.PasswordResetSerializer',
'password_reset_confirm': 'djoser.serializers.PasswordResetConfirmSerializer',
'password_reset_confirm_retype': 'djoser.serializers.PasswordResetConfirmRetypeSerializer',
'set_password': 'djoser.serializers.SetPasswordSerializer',
'set_password_retype': 'djoser.serializers.SetPasswordRetypeSerializer',
'set_username': 'djoser.serializers.SetUsernameSerializer',
'set_username_retype': 'djoser.serializers.SetUsernameRetypeSerializer',
'user_registration': 'djoser.serializers.UserRegistrationSerializer',
'user': 'djoser.serializers.UserSerializer',
'token': 'djoser.serializers.TokenSerializer',
}
So in my case, I put this in settings.py
:
DJOSER = {
'SERIALIZERS': {
'user_registration': 'account.serializers.UserSerializer',
},
}
and the endpoing http://127.0.0.1:8000/auth/register/
is now showing the fields I wanted it to. Hope that helps someone else!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With