django-rest-passwordreset icon indicating copy to clipboard operation
django-rest-passwordreset copied to clipboard

User matching query does not exist.

Open ksvoboda opened this issue 4 years ago • 6 comments

Hi, So, I am trying to get this working, but one thing is not working for me. When I reset password, I get response "OK", but when I try to login I get error: "User matching query does not exist." I really can't figure out why I get this error. I think I did everything right by the guide, only difference is that I don't send email, but I only print it in console. It looks like it make the user unaccesible, I still see the user in django admin. Let me know if/what code you need.

Has anyone any idea? Thanks

ksvoboda avatar Jun 17 '20 20:06 ksvoboda

That's because your User model now has a new related field "password_reset_tokens" added.

I believe all your pre-existing users in your database were created before you using this package, those users don't have the related field included. Hence, when you are querying for the users from database, no user is matching to your current new User model with the related field.

To solve this, probably deleting all your pre-existing users in database and start fresh would be the easier way ONLY if you don't wanna keep those users anymore.

jamehii avatar Jun 28 '20 01:06 jamehii

Thanks for your reply, but I actually deleted the whole database and migrated again before using it. I did it multiple times, but it didn’t help.

Any other idea please? Thanks

ksvoboda avatar Jun 28 '20 13:06 ksvoboda

@ksvoboda

There are 2 ways:

1st way Unregister your User model in the admin, then register again

If that doesn't work, then 2nd way:

Did you explicitly create ResetPasswordToken and link it to each of your "User" model instance ? If you didn't, that probably would cause login issue.

You can do it with "post_save" signal:

from django.db.models.signals import post_save
from django.dispatch import receiver
from django_rest_passwordreset.models import ResetPasswordToken

@reciever(post_save, sender=settings.AUTH_USER_MODEL)
on_user_post_save(sender, instance, created, **kwargs):
    if created:
        # Only do this once on each creation of user instance
        ResetPasswordToken.objects.create(user=instance)

Again, you might need to clean up your database and migrate again. Then you can try to login see if it works.

jamehii avatar Jun 29 '20 01:06 jamehii

So, if I understand it right, I just deleted User model in admin.py and then put it back. That didn't help. And the second way I just pasted your code in signals.py (I think you forgot def before on_user_post_save), still getting same error. Also VSCode is warning me that Class 'ResetPasswordToken' has no 'objects' member

But now, out of nowhere I get different error, now in console when trying to run the app.

File "/Users/krystofsvoboda/Documents/GitHub/project-tracker-api/myvenv/lib/python3.7/site-packages/django/core/urlresolvers.py", line 320
  except Resolver404, e:
                    ^
SyntaxError: invalid syntax

I wasn't able to fing any working solution on Google to this problem.

Thanks

ksvoboda avatar Jun 29 '20 07:06 ksvoboda

To unregister meaning in your admin.py, you will will have something below:

#unregister first admin.site.unregister(YourUserModel)

#then register again admin.site.register(YourUserModel)

jamehii avatar Jun 29 '20 11:06 jamehii

@ksvoboda have you been able to resolve this issue with @jamehii's help?

anx-cbenke avatar Aug 03 '20 11:08 anx-cbenke