Friday, October 7, 2011

Strange traceback: DoesNotExist not callable

Hallöchen!

I examine a traceback that ends with

...

File "/usr/lib/python2.6/dist-packages/django/db/models/query.py", line 351,
in get
% (self.model._meta.object_name, num, kwargs))

TypeError: 'DoesNotExist' object is not callable

The error-triggering source code in query.py says

def get(self, *args, **kwargs):
"""
Performs the query and returns a single object matching the given
keyword arguments.
"""
clone = self.filter(*args, **kwargs)
if self.query.can_filter():
clone = clone.order_by()
num = len(clone)
if num == 1:
return clone._result_cache[0]
if not num:
raise self.model.DoesNotExist("%s matching query does not exist."
% self.model._meta.object_name)
raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
% (self.model._meta.object_name, num, kwargs))


The last line triggers the error. This is strange for two reasons:
First, DoesNotExist should be callable for all models. And
secondly, there is no DoesNotExist in the last line but a
MultipleObjectsReturned.

My source code that is responsible for the error is:

try:
sample = self.samples.get() # Here, the TypeError occurs
except (Sample.DoesNotExist, Sample.MultipleObjectsReturned):
pass

"self" is a model instance. "samples" is a M2M field of "self" to
the class "Sample". In my case, ther are 20 samples connected with
the "self" instances, so raising a MultipleObjectsReturned would be
correct.

Does anybody have an idea what is going on here? How should I
proceed with debugging?

Tschö,
Torsten.

--
Torsten Bronger Jabber ID: torsten.bronger@jabber.rwth-aachen.de
or http://bronger-jmp.appspot.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment