I'am new to Django and DRF, but I'am trying to use DRF's Serializer to override djosers default user registration behavior.
(djoser = a DRF lib for user registration/login/password reset etc.)
I've djosers view, that uses a serializer to create user objectsdef perform_create(self, serializer):
user = serializer.save()
My idea was to override this serializer to achieve the following:
- create the user
- create an account object in parallel
- login the user
- return the account object alongside with the auth token in one response
The last point gives me trouble because I don't know how to achieve such custom behavior in the serializer. I made the input fields readonly, so they are not included in my response. In the save method a create a user + account object, logging in the user and then I return the user (which is needed by the view)
How do I serialize just the created account object with the created token string into one response?
This is my serializer (simplified and stripped from some stuff, but that's basically it)
class UserRegistrationSerializer(serializers.Serializer):
email = serializers.EmailField(write_only=True)
# some other fields
password = serializers.CharField(style={'input_type': 'password'},
write_only=True,
validators=settings.get('PASSWORD_VALIDATORS')
)
# this should be the output
account = Account(read_only=True)
def save(self):
user = User(email=self.validated_data['email'])
user.set_password(self.validated_data['password'])
user.save()
account = Account(user=user)
token = #logging in my user
return user