When I am using this queryset I am not getting any error and it's returning the user id
UserProfile.objects.get(forget_password_token=forget_password_token)
print(user.user_id)
>>>19
But when I am using this queryset UserProfile.objects.filter(forget_password_token=forget_password_token)
why I am getting this error QuerySet' object has no attribute 'user_id'
what is difference between get and filter in Django ? why not working filter method here?
When user putting wrong code in input fields then it's rising this error
UserProfile matching query does not exist.
CodePudding user response:
With .get() the return the instance of the object found so you can check directly user_id. With .filter() the rturn is a query set object contain all instances of ogject found (you can compare it to a list of results with some différences as it's an object type Queryset) So if you Want to check user_id, you have first to iterate all élément of the queryset
test = UserProfile.objects.filter(forget_password_token=forget_password_token)
for result in test:
print(test.user_id)
Another way can be to return the result in a list with .values_list()
test = UserProfile.objects.filter(forget_password_token=forget_password_token).values_list('user_id')