I am using django's custom user model. I want to query a user object based on email attribute. I am doing like:

user  = User.objects.get(email = email)

but it gives me error

File "D:\stocksapp\winenv\lib\site-packages\django\db\models\query.py", line 399, in get self.model._meta.object_name main.models.Code.DoesNotExist: Code matching query does not exist.

1 Answers

scomes On

The User.objects.get function will throw an error if nothing matches the query. You can either use a try/except block to handle this error, or do something like:

user = None
users = User.objects.filter(email=email)
if users.exists():
    user = user.first()