Say I have a table People, is there a way to just quickly check if a People object exists with a name of 'Fred'? I know I can query
People.objects.filter(Name='Fred')
and then check the length of the returned result, but is there a way to do it in a more elegant way?
Update:
As mentioned in more recent answers, since Django 1.2 you can use the
exists()method instead (link).Original Answer:
Dont’ use len() on the result, you should use
People.objects.filter(Name='Fred').count(). According to the django documentation,source: Django docs