I'd like to simply do the following without raising an exception if the item doesn't exist:
User.objects.get(email_address = email_address)
Is there a shortcut for this in Django? I just want to check if a user exists with a given email address.
I'd like to simply do the following without raising an exception if the item doesn't exist:
User.objects.get(email_address = email_address)
Is there a shortcut for this in Django? I just want to check if a user exists with a given email address.
You could use the queryset method exists()
.
From the django docs:
exists()
Returns True if the QuerySet contains any results, and False if not.
In your case:
User.objects.filter(email_address=email_address).exists()
If there were more than one user with this email address, exists
would still return True
, whereas the get()
would raise a User.MultipleObjectsTeturned
exception.