Suppose I have a model called User
, with a field called name
. How can I determine whether a User
instance, with a particular name Andrew
, already exists in the database?
I could do:
matched_users = User.objects.filter(name = 'Andrew')
if matched_users.count() == 0:
# It does not exist
else:
# It does exist
But is there a way I can do this in one line, without having to retrieve all instances and then count them?