#17664: Smart `if` tag silencing exceptions plus `QuerySet` caching equals buggy
behaviour.
-------------------------------------+-------------------------------------
     Reporter:  mrmachine            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Uncategorized        |                  Version:  SVN
     Severity:  Normal               |               Resolution:
     Keywords:  smart if tag         |             Triage Stage:
  queryset exception silenced        |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by krzysiumed):

 mrmachine, look at example:
 {{{
 >>> invalid_qs = Book.objects.order_by('invalid_field') # no error occures
 >>> type(invalid_qs)
 <class 'django.db.models.query.QuerySet'>
 >>> bool(invalid_qs) # this will raise exception
 Traceback (most recent call last):
 (... traceback ...)
 FieldError: Cannot resolve keyword 'invalid_field' into field. Choices
 are: id, membership, name, part
 }}}

 The queryset is evaluated in both situations -- `{% if qs %}` as well as
 `{% if not qs %}`, but it is evaluated in different places in python code.

 Queryset from `{% if qs %}` is evaluated here:
 
https://code.djangoproject.com/browser/django/trunk/django/template/defaulttags.py#L280
 and this line raises our `FieldError`. There is no try-except so the
 exception is not caught and we can see traceback.

 Queryset from `{% if not qs %}` is evaluated here:
 
https://code.djangoproject.com/browser/django/trunk/django/template/smartif.py#L98
 (and line 83). Line 83:
 {{{
 return func(context, self.first)
 }}}
 calls lambda function from line 98:
 {{{
 lambda context, x: not x.eval(context))
 }}}
 `x.eval(context)` is our `QuerySet`. Next, we want to negate the queryset
 because of `not` operator, so `bool(queryset)` is computed and it raises
 exception. We back to line 83 of `smartif.py`, where the lambda-function
 was called. As you can see, there is `except` clause and it catches every
 exception including our `FieldError`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/17664#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

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

Reply via email to