#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070182a7da7ea1-bd6eef31-271a-4384-a557-9de160fdd178-000000%40eu-central-1.amazonses.com.

Reply via email to