[Django] #24858: get_foo_display with the ArrayField

52 views
Skip to first unread message

Django

unread,
May 26, 2015, 11:54:08 AM5/26/15
to django-...@googlegroups.com
#24858: get_foo_display with the ArrayField
----------------------------------+-----------------
Reporter: MounirMesselmeni | Owner:
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+-----------------
I think using ArrayField as a many choices field would be awesome.
Passing choices to ArrayField works fine with MultipleChoiceField on the
form.
But calling get_foo_display return a TypeError: unhashable type: 'list',
maybe this method need to check if the field is an ArrayField it can
return a string representation of the choices separated by comma.

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

Django

unread,
May 26, 2015, 12:42:47 PM5/26/15
to django-...@googlegroups.com
#24858: get_foo_display with the ArrayField
----------------------------------+--------------------------------------

Reporter: MounirMesselmeni | Owner:
Type: Uncategorized | Status: new
Component: contrib.postgres | 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:

Could you please add a code snippet of the expected behavior?

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

Django

unread,
May 27, 2015, 3:21:39 AM5/27/15
to django-...@googlegroups.com
#24858: get_foo_display with the ArrayField
----------------------------------+--------------------------------------

Reporter: MounirMesselmeni | Owner:
Type: Uncategorized | Status: new
Component: contrib.postgres | 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
----------------------------------+--------------------------------------
Description changed by MounirMesselmeni:

Old description:

> I think using ArrayField as a many choices field would be awesome.
> Passing choices to ArrayField works fine with MultipleChoiceField on the
> form.
> But calling get_foo_display return a TypeError: unhashable type: 'list',
> maybe this method need to check if the field is an ArrayField it can
> return a string representation of the choices separated by comma.

New description:

I think using ArrayField as a many choices field would be awesome.
Passing choices to ArrayField works fine with MultipleChoiceField on the
form.
But calling get_foo_display return a TypeError: unhashable type: 'list',
maybe this method need to check if the field is an ArrayField it can
return a string representation of the choices separated by comma.


{{{#!python
class Example(models.Model):
CHOICES = (
(1, 'value1'),
(2, 'value2'),
(3, 'value3'),
)

multi_choices_array = ArrayField(
base_field=models.IntegerField(),
choices=CHOICES,
)

example = Example.objects.create(multi_choices_array= [1, 2])
example.get_multi_choices_array_display()
# Will raise a Type Error exception

}}}

--

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

Django

unread,
May 27, 2015, 3:25:51 AM5/27/15
to django-...@googlegroups.com
#24858: get_foo_display with the ArrayField
----------------------------------+--------------------------------------

Reporter: MounirMesselmeni | Owner:
Type: Uncategorized | Status: new
Component: contrib.postgres | 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
----------------------------------+--------------------------------------
Description changed by MounirMesselmeni:

Old description:

> I think using ArrayField as a many choices field would be awesome.


> Passing choices to ArrayField works fine with MultipleChoiceField on the
> form.
> But calling get_foo_display return a TypeError: unhashable type: 'list',
> maybe this method need to check if the field is an ArrayField it can
> return a string representation of the choices separated by comma.
>

> {{{#!python


> class Example(models.Model):
> CHOICES = (
> (1, 'value1'),
> (2, 'value2'),
> (3, 'value3'),
> )
>
> multi_choices_array = ArrayField(
> base_field=models.IntegerField(),
> choices=CHOICES,
> )
>
> example = Example.objects.create(multi_choices_array= [1, 2])
> example.get_multi_choices_array_display()
> # Will raise a Type Error exception
>
> }}}

New description:

I think using ArrayField as a many choices field would be awesome.
Passing choices to ArrayField works fine with MultipleChoiceField on the
form.
But calling get_foo_display return a TypeError: unhashable type: 'list',
maybe this method need to check if the field is an ArrayField it can
return a string representation of the choices separated by comma.

{{{#!python
class Example(models.Model):
CHOICES = (
(1, 'value1'),
(2, 'value2'),
(3, 'value3'),
)

multi_choices_array = ArrayField(
base_field=models.IntegerField(),
choices=CHOICES,
)

# Adding this method will show the values
def multi_choices_array_display(self):
result = ''
choices = dict(self.CHOICES)
for index, value in enumerate(self.multi_choices_array):
result += "{0}".format(choices[value])
if not index == len(self.multi_choices_array) - 1:
result += ', '
return result

example = Example.objects.create(multi_choices_array= [1, 2])
example.get_multi_choices_array_display()
# Will raise a Type Error exception

example.multi_choices_array_display()
# Will print 'value1, value2'

}}}

--

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

Django

unread,
May 27, 2015, 2:04:56 PM5/27/15
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner:
Type: New feature | Status: new
Component: contrib.postgres | Version: master
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):

* type: Uncategorized => New feature
* version: 1.8 => master
* stage: Unreviewed => Accepted


Comment:

Not sure about feasibility, but the request makes sense. If infeasible, it
would be nice if ArrayField didn't create the method which doesn't work.

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

Django

unread,
May 28, 2015, 10:49:26 PM5/28/15
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner:
Type: New feature | Status: new
Component: contrib.postgres | Version: master
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 chedi):

* Attachment "get_FIELD_display.patch" added.

get_FIELD_display fix for the ArrayField field

Django

unread,
May 28, 2015, 10:51:24 PM5/28/15
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner:
Type: New feature | Status: new
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by chedi):

* cc: chedi (added)
* has_patch: 0 => 1
* needs_tests: 0 => 1
* easy: 0 => 1


Comment:

There is two problems here, the first has to do with the fact that lists
are not hashable, so get_multi_choices_array_display
will fail when trying to hash the python object. The second one is more or
less the same thing but has to do with the
elements of the choice object, it to has to have the choice element of the
tuple hashable.

A quick workaround would be to pass tuples in both instances would be to
test if the object is an instance of collections.Hashable and call a field
specific function to convert the value if it's not the case.

I hacked a some simple code to show the idea, and it seems to work ok.

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

Django

unread,
Jun 4, 2015, 6:27:24 AM6/4/15
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner: asser
Type: New feature | Status: assigned

Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by asser):

* status: new => assigned
* owner: => asser


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

Django

unread,
Jun 9, 2015, 12:54:21 PM6/9/15
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner: asser
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by asser):

I think the usage in the description is slightly wrong; if specifying
choices for an ArrayField the choices themselves should be lists:


{{{#!python
class Example(models.Model):
CHOICES = (

([1, 2], 'value1'),
([3, 4], 'value2'),
([5], 'value3'),


)
multi_choices_array = ArrayField(
base_field=models.IntegerField(),
choices=CHOICES,
)
}}}

To have an ArrayField that only accepts certain values on the base field,
the usage would probably be:

{{{#!python
class Example(models.Model):
CHOICES = (
(1, 'value1'),
(2, 'value2'),
(3, 'value3'),
)
multi_choices_array = ArrayField(

base_field=models.IntegerField(choices=CHOICES),
)
}}}

Then supposedly you could add a forms.MultipleChoiceField to fill in the
array's values?

Neither of the two forms above currently work. I've managed to modify the
postgres-specific code to handle the first case (choices being lists on
the ArrayField) based on chedi's patch -- see
https://github.com/asser/django/tree/ticket_24858 -- and am currently
trying to figure out what goes wrong in the validation handling of the
second case (choices on the base field).

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

Django

unread,
Jul 4, 2015, 8:50:08 AM7/4/15
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner: asser
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1
* needs_tests: 1 => 0
* easy: 1 => 0


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

Django

unread,
Apr 2, 2016, 10:42:38 AM4/2/16
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner: asser
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by asser):

Open PR at https://github.com/django/django/pull/6381

Fixes the issue with `get_FIELD_display`.

ArrayField seems to work fine with choices on the base field, unless a
forms.MultipleChoiceField is used to access it, but that feels like a
separate issue to this one (I think it needs work on the validation in the
form fields instead).

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

Django

unread,
Apr 2, 2016, 11:19:49 AM4/2/16
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+------------------------------------
Reporter: MounirMesselmeni | Owner: asser
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

Comment (by timgraham):

I left comments for improvement on the pull request. Please uncheck "Patch
needs improvement" when you update it so that the ticket goes on the
review queue.

--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:10>

Django

unread,
Nov 5, 2016, 7:32:12 AM11/5/16
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+--------------------------------------
Reporter: Mounir | Owner: Artur Smęt

Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Artur Smęt):

* owner: Asser Schrøder Femø => Artur Smęt


--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:11>

Django

unread,
Nov 5, 2016, 9:43:00 AM11/5/16
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+--------------------------------------
Reporter: Mounir | Owner: Artur Smęt
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | 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 Artur Smęt):

* needs_better_patch: 1 => 0


Comment:

Improved PR reopened at https://github.com/django/django/pull/7490

--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:12>

Django

unread,
Nov 17, 2016, 12:12:39 PM11/17/16
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
----------------------------------+--------------------------------------
Reporter: Mounir | Owner: Artur Smęt
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:13>

Django

unread,
Nov 6, 2019, 6:00:29 PM11/6/19
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
-------------------------------------+-------------------------------------
Reporter: Mounir | Owner: Hasan
| Ramezani

Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | 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 Hasan Ramezani):

* owner: Artur Smęt => Hasan Ramezani


* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:14>

Django

unread,
Nov 7, 2019, 6:24:33 AM11/7/19
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
-------------------------------------+-------------------------------------
Reporter: Mounir | Owner: Hasan
| Ramezani
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:15>

Django

unread,
Nov 7, 2019, 11:02:44 AM11/7/19
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
-------------------------------------+-------------------------------------
Reporter: Mounir | Owner: Hasan
| Ramezani
Type: New feature | Status: assigned
Component: contrib.postgres | Version: master
Severity: Normal | 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 Hasan Ramezani):

* needs_better_patch: 1 => 0


* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:16>

Django

unread,
Nov 8, 2019, 5:36:05 AM11/8/19
to django-...@googlegroups.com
#24858: Add support for get_foo_display() with ArrayField
-------------------------------------+-------------------------------------
Reporter: Mounir | Owner: Hasan
| Ramezani
Type: New feature | Status: closed
Component: contrib.postgres | Version: master
Severity: Normal | 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:"153c7956f84ef4bd203e750f8669f70b6f7fa3f7" 153c7956]:
{{{
#!CommitTicketReference repository=""
revision="153c7956f84ef4bd203e750f8669f70b6f7fa3f7"
Fixed #24858 -- Added support for get_FOO_display() to ArrayField and
RangeFields.

_get_FIELD_display() crashed when Field.choices was unhashable.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:17>

Reply all
Reply to author
Forward
0 new messages