Skip to content
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

How do I use this lib with User model's password field? #111

Open
amangup opened this issue May 6, 2020 · 0 comments
Open

How do I use this lib with User model's password field? #111

amangup opened this issue May 6, 2020 · 0 comments

Comments

@amangup
Copy link

amangup commented May 6, 2020

I have a custom User model with create_user method on User.objects, in an implementation similar to one shown here:
https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#a-full-example

If I user your Mixins, the password field is updated as a regular character field directly on the model, both on create() and update() (which is obvious).

For update(), I thought I could write my own update logic for password and then call super() like so:

    def update(self, instance, validated_data):
        user = self.context['request'].user
        new_password = validated_data.pop('password', None)
        if new_password and user.email == instance.email:
            user.set_password(new_password)
            user.save()

        return super(UserSerializer, self).update(instance, validated_data)

but I'm getting an UniqueViolation error for the email field (it has an unique constraint, for obvious reasons). It's curious that my update() method is not even being called.

Even if the update() solution can be tweaked to work, I can't think of any solution for create().

Is there a way to solve this issue?

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

No branches or pull requests

1 participant