--
Ticket URL: <https://code.djangoproject.com/ticket/24858>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* 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>
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>
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>
* 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>
* Attachment "get_FIELD_display.patch" added.
get_FIELD_display fix for the ArrayField field
* 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>
* status: new => assigned
* owner: => asser
--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:6>
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>
* needs_better_patch: 0 => 1
* needs_tests: 1 => 0
* easy: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:8>
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>
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>
* owner: Asser Schrøder Femø => Artur Smęt
--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:11>
* 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>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:13>
* owner: Artur Smęt => Hasan Ramezani
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:14>
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:15>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/24858#comment:16>
* 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>