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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With