iMerica / dj-rest-auth

Authentication for Django Rest Framework
https://dj-rest-auth.readthedocs.io/en/latest/index.html
MIT License
1.63k stars 304 forks source link

Why commit=False in RegisterSerialzer? #523

Open sowinski opened 1 year ago

sowinski commented 1 year ago

Hi,

I use Django Allauth in my projects and wanted to extend my website with this rest endpoint implementation.

In /dj_rest_auth/registration/serialziers.py you set line 263 the commit option to False. Why?

   def save(self, request):
        adapter = get_adapter()
        user = adapter.new_user(request)
        self.cleaned_data = self.get_cleaned_data()
        user = adapter.save_user(request, user, self, commit=False)
        if "password1" in self.cleaned_data:
            try:
                adapter.clean_password(self.cleaned_data['password1'], user=user)
            except DjangoValidationError as exc:
                raise serializers.ValidationError(
                    detail=serializers.as_serializer_error(exc)
            )
        user.save()
        self.custom_signup(request, user)
        setup_user_email(request, user, [])
        return user

This beaks my code, because in my custom adapter i do some actions with the user object, but if commit=False I can not add a foreign key to a new objects which is created in my custom adapter.

Thank you

Dresdn commented 10 months ago

I know this is late, but looks like the logic behind it is to return an error when there's a password error, as django-allauth's save_user() by default will just set an unusable password.

You bring up a good point, and maybe this should be changed to user = adapter.save_user(request, user, self), which would then allow you to add actions in your adapter after saving the user object.