Opened 10 years ago
Closed 5 years ago
#24858 closed New feature (fixed)
Add support for get_foo_display() with ArrayField
Reported by: | Mounir | Owned by: | Hasan Ramezani |
---|---|---|---|
Component: | contrib.postgres | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | chedi | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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.
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'
Attachments (1)
Change History (18)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Description: | modified (diff) |
---|
comment:3 by , 10 years ago
Description: | modified (diff) |
---|
comment:4 by , 10 years ago
Summary: | get_foo_display with the ArrayField → Add support for get_foo_display() with ArrayField |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → New feature |
Version: | 1.8 → master |
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.
by , 10 years ago
Attachment: | get_FIELD_display.patch added |
---|
get_FIELD_display fix for the ArrayField field
comment:5 by , 10 years ago
Cc: | added |
---|---|
Easy pickings: | set |
Has patch: | set |
Needs tests: | set |
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.
comment:6 by , 10 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:7 by , 10 years ago
I think the usage in the description is slightly wrong; if specifying choices for an ArrayField the choices themselves should be lists:
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:
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).
comment:8 by , 9 years ago
Easy pickings: | unset |
---|---|
Needs tests: | unset |
Patch needs improvement: | set |
comment:9 by , 9 years ago
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).
comment:10 by , 9 years ago
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.
comment:11 by , 8 years ago
Owner: | changed from | to
---|
comment:12 by , 8 years ago
Patch needs improvement: | unset |
---|
Improved PR reopened at https://github.com/django/django/pull/7490
comment:13 by , 8 years ago
Patch needs improvement: | set |
---|
comment:14 by , 5 years ago
Owner: | changed from | to
---|---|
Patch needs improvement: | unset |
comment:15 by , 5 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
comment:16 by , 5 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
Could you please add a code snippet of the expected behavior?