[Django] #31154: Enumeration Types not usable in templates

28 views
Skip to first unread message

Django

unread,
Jan 9, 2020, 10:31:49 AM1/9/20
to django-...@googlegroups.com
#31154: Enumeration Types not usable in templates
-------------------------------------------------+------------------------
Reporter: Adam (Chainz) Johnson | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------------+------------------------
The new [enumeration
types](https://docs.djangoproject.com/en/3.0/ref/models/fields
/#enumeration-types) are great but can't be used in Django templates due
to their being callable. For example this doesn't work:

{{{
{% if student.year_in_school == YearInSchool.FRESHMAN %}
}}}

This is because YearInSchool, being a class, is callable, and Django
Templates always call callables with no arguments. The call fails because
the required `value` argument is missing.

The easy solution would be to declare `do_not_call_in_templates = True` on
the various Choices classes.

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

Django

unread,
Jan 9, 2020, 10:37:28 AM1/9/20
to django-...@googlegroups.com
#31154: Enumeration Types not usable in templates
-------------------------------------+-------------------------------------
Reporter: Adam (Chainz) | Owner: Adam
Johnson | (Chainz) Johnson
Type: Bug | Status: assigned

Component: Template system | Version: 3.0
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 Adam (Chainz) Johnson):

* owner: nobody => Adam (Chainz) Johnson
* status: new => assigned


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

Django

unread,
Jan 9, 2020, 2:23:02 PM1/9/20
to django-...@googlegroups.com
#31154: Enumeration Types are not usable in templates.

-------------------------------------+-------------------------------------
Reporter: Adam (Chainz) | Owner: Adam
Johnson | (Chainz) Johnson
Type: Bug | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* severity: Normal => Release blocker
* component: Template system => Database layer (models, ORM)
* has_patch: 0 => 1
* stage: Unreviewed => Accepted


Old description:

> The new [enumeration
> types](https://docs.djangoproject.com/en/3.0/ref/models/fields
> /#enumeration-types) are great but can't be used in Django templates due
> to their being callable. For example this doesn't work:
>
> {{{
> {% if student.year_in_school == YearInSchool.FRESHMAN %}
> }}}
>
> This is because YearInSchool, being a class, is callable, and Django
> Templates always call callables with no arguments. The call fails because
> the required `value` argument is missing.
>
> The easy solution would be to declare `do_not_call_in_templates = True`
> on the various Choices classes.

New description:

The new [https://docs.djangoproject.com/en/3.0/ref/models/fields
/#enumeration-types enumeration types] are great but can't be used in


Django templates due to their being callable. For example this doesn't
work:

{{{
{% if student.year_in_school == YearInSchool.FRESHMAN %}
}}}

This is because YearInSchool, being a class, is callable, and Django
Templates always call callables with no arguments. The call fails because
the required `value` argument is missing.

The easy solution would be to declare `do_not_call_in_templates = True` on
the various Choices classes.

--

Comment:

[https://github.com/django/django/pull/12304 PR]

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

Django

unread,
Jan 10, 2020, 3:13:25 AM1/10/20
to django-...@googlegroups.com
#31154: Enumeration Types are not usable in templates.
-------------------------------------+-------------------------------------
Reporter: Adam (Chainz) | Owner: Adam
Johnson | (Chainz) Johnson
Type: Bug | Status: closed

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"5166097d7c80cab757e44f2d02f3d148fbbc2ff6" 5166097]:
{{{
#!CommitTicketReference repository=""
revision="5166097d7c80cab757e44f2d02f3d148fbbc2ff6"
Fixed #31154 -- Added support for using enumeration types in templates.

Enumeration helpers are callables, so the template system tried to call
them with no arguments.

Thanks Rupert Baker for helping discover this.
}}}

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

Django

unread,
Jan 10, 2020, 3:14:05 AM1/10/20
to django-...@googlegroups.com
#31154: Enumeration Types are not usable in templates.
-------------------------------------+-------------------------------------
Reporter: Adam (Chainz) | Owner: Adam
Johnson | (Chainz) Johnson
Type: Bug | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"16297e7d5e3a6b0287d114e95d1d544995618c93" 16297e7]:
{{{
#!CommitTicketReference repository=""
revision="16297e7d5e3a6b0287d114e95d1d544995618c93"
[3.0.x] Fixed #31154 -- Added support for using enumeration types in
templates.

Enumeration helpers are callables, so the template system tried to call
them with no arguments.

Thanks Rupert Baker for helping discover this.

Backport of 5166097d7c80cab757e44f2d02f3d148fbbc2ff6 from master
}}}

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

Reply all
Reply to author
Forward
0 new messages