[Django] #25600: Template `if` tag behavior change with 1.8, OneToOneField, RelatedObjectDoesNotExist is True?

17 views
Skip to first unread message

Django

unread,
Oct 23, 2015, 6:01:52 AM10/23/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+--------------------
Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
We discovered a strange behavior change when upgrading from Django 1.7 to
1.8.

Let's assume we have a {{{OneToOneField}}} like here:

{{{#!python
class Customer(models.Model):
name = models.CharField(max_length=100)


class Project(models.Model):
customer = models.OneToOneField(Customer)
title = models.CharField(max_length=100)
}}}

the template gets the {{{Customer}}} instance, without a project, in the
context.

{{{
{% if customer.project %}
{# on 1.8 we get here #}
{% else %}
{# on 1.7 we get here #}
{% endif %}
}}}

when debugging and trying to access {{{customer.project}}} in a django
shell, we get a {{{RelatedObjectDoesNotExist}}} in both versions, so this
does not seem to have changed.

So to us it looks like the behavior of the {{{if}}} tag in templates
changed.

I'm happy to provide more info as needed, or test stuff. Or even write a
PR / Fix if someone points me to possible places where this could have
been changed / broken.

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

Django

unread,
Oct 23, 2015, 9:32:27 AM10/23/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+--------------------------------------

Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

I can't reproduce that -- I'm always seeing the else branch on master and
stable/1.8.x. Maybe you could provide a failing test case for Django's
test suite?

--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:1>

Django

unread,
Oct 23, 2015, 12:53:13 PM10/23/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+--------------------------------------

Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by syphar):

wanted to play around a little to find the point where it stopped working
(in our codebase).

I starts happening when we set {{{TEMPLATE_STRING_IF_INVALID}}} to any
value (with {{{%s}}} or without)

Out of curiosity I tried if it's related to the old
({{{TEMPLATE_STRING_IF_INVALID}}} or the new ({{{string_if_invalid}}}
option in the {{{TEMPLATES}}} config, but it happens always.

--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:2>

Django

unread,
Oct 23, 2015, 12:53:31 PM10/23/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+--------------------------------------

Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------

Comment (by syphar):

I can still try do write a failing testcase if it helps

--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:3>

Django

unread,
Oct 23, 2015, 1:29:33 PM10/23/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+------------------------------------

Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by timgraham):

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Okay, making that setting a non-empty value is the piece I was missing to
reproduce. Bisected the change to
0dd05c9e66ebb5cb97136f84373f43582783e1a6. It's not immediately obvious to
me what the resolution should be, but at least some documentation could be
added if we decide not to change the behavior.

--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:4>

Django

unread,
Oct 28, 2015, 11:16:13 AM10/28/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+------------------------------------

Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by timgraham):

The `RelatedObjectDoesNotExist` exception is a subclass of
`AttributeError` -- that's why it's hitting that logic added in the commit
noted in the previous comment. We end up checking `{% if
<string_if_invalid> %}` which passes for a non-empty `string_if_invalid`.
I'm not sure we should add special handling of `RelatedObjectDoesNotExist`
in the template engine to restore the old behavior (and I'm not sure how
to since `RelatedObjectDoesNotExist` is a dynamically generated exception
based on the related descriptors).

I think the usage of `string_if_invalid` is somewhat
unreliable/unpredictable anyway and therefore discouraged but I'm not too
sure as I haven't used it myself.

--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:5>

Django

unread,
Oct 29, 2015, 12:54:24 PM10/29/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+------------------------------------

Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by felixxm):

* cc: felisiak.mariusz@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:6>

Django

unread,
Nov 12, 2015, 11:25:29 AM11/12/15
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+------------------------------------

Reporter: syphar | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by timgraham):

* severity: Release blocker => Normal


Comment:

Per [https://groups.google.com/d/topic/django-
developers/BLW5fCTDYHA/discussion discussion on django-developers],
demoting from a release blocker since `string_if_invalid` is only intended
for debugging. Inconsistencies in how the `{% if %}` tag silences
exceptions (#17664) may be related.

--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:7>

Django

unread,
Feb 19, 2018, 4:07:51 PM2/19/18
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+-----------------------------------------
Reporter: Denis Cornehl | Owner: Robert Roskam
Type: Bug | Status: assigned

Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------
Changes (by Robert Roskam):

* status: new => assigned
* owner: nobody => Robert Roskam


--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:8>

Django

unread,
Feb 19, 2018, 4:08:34 PM2/19/18
to django-...@googlegroups.com
#25600: Template `if` tag behavior change with 1.8, OneToOneField,
RelatedObjectDoesNotExist is True?
---------------------------------+------------------------------------
Reporter: Denis Cornehl | Owner: (none)

Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Robert Roskam):

* status: assigned => new
* owner: Robert Roskam => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/25600#comment:9>

Reply all
Reply to author
Forward
0 new messages