Re: [Django] #33927: Rendering a read-only ArrayField with choices crashes. (was: Rendering an ArrayField as read-only in admin raises TypeError)

13 views
Skip to first unread message

Django

unread,
Aug 16, 2022, 2:11:33 PM8/16/22
to django-...@googlegroups.com
#33927: Rendering a read-only ArrayField with choices crashes.
-------------------------------+------------------------------------
Reporter: David Svenson | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
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 Mariusz Felisiak):

* stage: Unreviewed => Accepted


Comment:

Thanks for the report. `ArrayField` must have `choices` to reproduce this
issue, e.g.
{{{#!python
field_2 = ArrayField(models.IntegerField(), choices=[
([1, 2, 3], "base choice"),
([1, 2], "1st choice"),
([2, 3], "2nd choice"),
])
}}}
We could try to call `make_hashable()` on the `flatchoices` and `value`:
{{{#!diff
diff --git a/django/contrib/admin/utils.py b/django/contrib/admin/utils.py
index 6cb549fb10..3e09153138 100644
--- a/django/contrib/admin/utils.py
+++ b/django/contrib/admin/utils.py
@@ -10,6 +10,7 @@ from django.db.models.deletion import Collector
from django.forms.utils import pretty_name
from django.urls import NoReverseMatch, reverse
from django.utils import formats, timezone
+from django.utils.hashable import make_hashable
from django.utils.html import format_html
from django.utils.regex_helper import _lazy_re_compile
from django.utils.text import capfirst
@@ -401,7 +402,7 @@ def display_for_field(value, field,
empty_value_display):
from django.contrib.admin.templatetags.admin_list import
_boolean_icon

if getattr(field, "flatchoices", None):
- return dict(field.flatchoices).get(value, empty_value_display)
+ return
dict(make_hashable(field.flatchoices)).get(make_hashable(value),
empty_value_display)
# BooleanField needs special-case null-handling, so it comes before
the
# general null test.
elif isinstance(field, models.BooleanField):
}}}
but this can cause a performance regression.

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

Reply all
Reply to author
Forward
0 new messages