Opened 2 years ago

Last modified 10 months ago

#24858 assigned New feature

Add support for get_foo_display() with ArrayField

Reported by: Mounir Owned by: Artur Smęt
Component: contrib.postgres Version: master
Severity: Normal Keywords:
Cc: chedi Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Mounir)

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)

get_FIELD_display.patch (2.5 KB) - added by chedi 2 years ago.
get_FIELD_display fix for the ArrayField field

Download all attachments as: .zip

Change History (14)

comment:1 Changed 2 years ago by Tim Graham

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

comment:2 Changed 2 years ago by Mounir

Description: modified (diff)

comment:3 Changed 2 years ago by Mounir

Description: modified (diff)

comment:4 Changed 2 years ago by Tim Graham

Summary: get_foo_display with the ArrayFieldAdd support for get_foo_display() with ArrayField
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature
Version: 1.8master

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.

Changed 2 years ago by chedi

Attachment: get_FIELD_display.patch added

get_FIELD_display fix for the ArrayField field

comment:5 Changed 2 years ago by chedi

Cc: chedi 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 Changed 2 years ago by Asser Schrøder Femø

Owner: set to Asser Schrøder Femø
Status: newassigned

comment:7 in reply to:  description Changed 2 years ago by Asser Schrøder Femø

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 Changed 2 years ago by Tim Graham

Easy pickings: unset
Needs tests: unset
Patch needs improvement: set

comment:9 Changed 18 months ago by Asser Schrøder Femø

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 Changed 18 months ago by Tim Graham

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 Changed 11 months ago by Artur Smęt

Owner: changed from Asser Schrøder Femø to Artur Smęt

comment:12 Changed 11 months ago by Artur Smęt

Patch needs improvement: unset

comment:13 Changed 10 months ago by Tim Graham

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top