Deep Shah Deep Shah - 1 month ago 8
Python Question

What is the exact usage of exists()?

I am using it as

if ModelName.objects.get(fieldname=value).exists():


I either get the error 'ModelName' object has no attribute 'exists' or it throws a
DoesNotExist
exception.

Related doc: https://docs.djangoproject.com/en/1.10/ref/models/querysets/#django.db.models.query.QuerySet.exists

Answer

You should apply it to queryset, not a single object, try:

if ModelName.objects.filter(fieldname=value).exists():

If queryset is not empty, this will return True.