Hi João and Torsten,

On 20/10/11 18:53, João Pedro Francese wrote:
I've just stumbled upon the same problem in my code. I have no clue on
why it happened either. Did you ever get any progress on investigating
this issue?

Regards,
Joao

On Oct 7, 12:31 pm, Torsten Bronger<bron...@physik.rwth-aachen.de>
wrote:
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'objectisnotcallable

The error-triggering source code in query.py says

     def get(self, *args, **kwargs):
         """
         Performs the query and returns a singleobjectmatching 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]
         ifnotnum:
             raise self.model.DoesNotExist("%s matching query doesnotexist."
                     % 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,DoesNotExistshould becallablefor all models.  And
secondly, there is noDoesNotExistin 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
I've been caught out by this error before. In my case it was because I was not catching exceptions correctly *elsewhere* in my code. In another view, I had something like

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

Without the parenthesis, it's equivalent to the following in Python 2.6+

    except Sample.DoesNotExist as Sample.MultipleObjectsReturned:

The instance of the DoesNotExist exception overwrites Sample.MultipleObjectsReturned!

When the same process is handles a different request later on, you get the type error because your code is trying to call the DoesNotExist instance which has replaced Sample.MultipleObjectsReturned.

tschüss,

Alasdair

--
Alasdair Nicol
Developer, MEMSET

mail: alasd...@memset.com
 web: http://www.memset.com/

Memset Ltd., registration number 4504980. 25 Frederick Sanger Road, Guildford, 
Surrey, GU2 7YD, UK.

--
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to