Skip to content

Return all serializer field values in RegisterSerializer#get_cleaned_data #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rubengrill
Copy link

RegisterSerializer#get_cleaned_data returns statically the fields username, password1 and email from self.validated_data.

That means that all subclasses of RegisterSerializer have to override this method as soon as they add other fields to the new serializer.

Would be nice, if adding new fields to the serializer is all there is to do.

Tried to add a new test, but it seems that REST_AUTH settings can't be overridden:

class CustomRegisterSerializer(RegisterSerializer):
    first_name = serializers.CharField(required=False, max_length=255, allow_blank=True)
    last_name = serializers.CharField(required=False, max_length=255, allow_blank=True)

...

    @override_settings(
        REST_AUTH_REGISTER_SERIALIZERS={
            'REGISTER_SERIALIZER': 'rest_auth.tests.test_api.CustomRegisterSerializer',
        }
    )
    def test_registration_with_name(self):
        self.post(self.register_url, data=self.REGISTRATION_DATA_WITH_NAME, status_code=201)

        new_user = get_user_model().objects.latest('id')

        self.assertEqual(new_user.first_name, self.REGISTRATION_DATA_WITH_NAME['first_name'])
        self.assertEqual(new_user.last_name, self.REGISTRATION_DATA_WITH_NAME['last_name'])

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 97.081% when pulling 6c0e2d4 on rubengrill:register-serializer into 3b80fcb on Tivix:master.

1 similar comment
@coveralls
Copy link

coveralls commented Oct 14, 2016

Coverage Status

Coverage increased (+0.01%) to 97.081% when pulling 6c0e2d4 on rubengrill:register-serializer into 3b80fcb on Tivix:master.

@rcanand
Copy link

rcanand commented Jul 19, 2017

@rubengrill - 👍 for PR - it worked for me.

You can override settings in test using settings:

class MyTestCase(TestCase):

    def test_custom_registration(self):
        with self.settings(REST_AUTH_REGISTER_SERIALIZERS={
            'REGISTER_SERIALIZER': 'rest_auth.tests.test_api.CustomRegisterSerializer',
        }):
        self.post(self.register_url, data=self.REGISTRATION_DATA_WITH_NAME, status_code=201)

        new_user = get_user_model().objects.latest('id')

        self.assertEqual(new_user.first_name, self.REGISTRATION_DATA_WITH_NAME['first_name'])
        self.assertEqual(new_user.last_name, self.REGISTRATION_DATA_WITH_NAME['last_name'])   

@colin-byrne-1
Copy link

can we get this merged?

@BarnabasSzabolcs
Copy link

Hi,
as a user of rest-auth, thanks for the contribution!
This repo is not maintained anymore, so the development moved to dj-rest-auth. (reference: #568)
It may be best, if you move this PR there. (and upgrade to using dj_rest_auth)

new repo link: https://github.com/jazzband/dj-rest-auth (I'm not the upkeeper of that repo, it just makes sense for me to help you merge your PR)

Many Thanks,
Barney

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants