#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 <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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/074.c089ca1f7ea61e110e0c2cdec1df8cb1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to