Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object has no attribute 'exists'

Tags:

django

I am using Django 1.3 and trying to use .exists() on a model entry but get the error listed below. Exists() was included in Django 1.2 so I should have access to it. I verified my version using django.get_version and it was okay. Querying MyModel based on pk only returns an entry but querying with .exists() throws an error. Do I need to imports something?

>>> m = MyModel.objects.get(pk=1)
>>> m
<MyModel: Model field entry 1>
>>> m = MyModel.objects.get(pk=1).exists()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'MyModel' object has no attribute 'exists'
like image 882
Roland Avatar asked Jun 19 '12 01:06

Roland


1 Answers

exists() is a method of a QuerySet.

get() returns a single model instance, and will raise an exception Entry.DoesNotExist if that instance doesn't exist. So you'll need to wrap this in a try/except block if you're not sure if an instance with that id exists.

like image 120
mVChr Avatar answered Nov 08 '22 06:11

mVChr