Re: SelectMultiple with Static Choices in admin Interface

2009-07-13 Thread Ian Clelland



On Jul 13, 10:03 am, itodd  wrote:
> Greetings,
>
> I'm trying to use a SelectMultiple with a set of static choices. I'm
> experiencing odd behavior when the number of choices exceeds 10. For
> example, take the following Model and ModelForm:
>
> class Project(models.Model):
>     RECIPIENTS = (
>         (1,'Brad'),
>         (2,'Fred'),
>         (3,'Tom'),
>         ...
>         (12,'Harry'),
>         (13,'Betty'),
>     )
>     recipients = models.CharField(max_length=255,choices=RECIPIENTS)
>
> class ProjectForm(forms.ModelForm):
>     recipients = forms.CharField(widget=forms.SelectMultiple
> (choices=Project.RECIPIENTS))
>
> If I select Harry (12) and Betty (13) then save, the correct list
> appears in the database column: [u'12',u'13']. However, when I edit
> the project in the admin interface, Brad (1), Fred (2) and Tom (3) are
> selected. What seems to be happening is that SelectMultiple is
> selecting 1,2,1 and 3 instead of 12 and 13.

Well, part of the problem is that django is storing the python string
representation of a list in the database, in your CharField. The
SelectMultiple widget is returning a list, and by storing that in a
CharField without any further processing, you are getting the repr()
of that list.

When you look at that in the admin (assuming that you've forced admin
to use a SelectMultiple widget as well,) the value is being passed as
a string (not a list) to the widget for rendering.

> Can anyone offer any advice? I have worked around this by using
> letters instead of numbers but this work-around is less than ideal.

If you use unique letters, rather than numbers, this will appear to
work, since the widget will be looking for single characters within a
string, which will (mostly) work. What should be happening, though, is
that something in your code should be taking the output of the
SelectMultiple widget (from the form) and serializing and
deserializing it properly (I doubt that the string "[u'12',u'13']" is
the most appropriate representation of the recipients field in the
database)

(I say "mostly" in the previous paragragph, because if one of your
choices uses the letter "u", then it may appear to be selected all of
the time, because of the python representation of unicode strings)

You probably want to be excluding the recipients field from the model
form, replacing it with another field which uses the multiple-select
widget. Then you can pre-set the value in the widget before you render
the form, and you can set the proper value for the CharField before
you save the model. In the admin, I would just leave recipients as a
CharField, without trying to force it into a multiple select.

Ian
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



SelectMultiple with Static Choices in admin Interface

2009-07-13 Thread itodd

Greetings,

I'm trying to use a SelectMultiple with a set of static choices. I'm
experiencing odd behavior when the number of choices exceeds 10. For
example, take the following Model and ModelForm:

class Project(models.Model):
RECIPIENTS = (
(1,'Brad'),
(2,'Fred'),
(3,'Tom'),
...
(12,'Harry'),
(13,'Betty'),
)
recipients = models.CharField(max_length=255,choices=RECIPIENTS)

class ProjectForm(forms.ModelForm):
recipients = forms.CharField(widget=forms.SelectMultiple
(choices=Project.RECIPIENTS))

If I select Harry (12) and Betty (13) then save, the correct list
appears in the database column: [u'12',u'13']. However, when I edit
the project in the admin interface, Brad (1), Fred (2) and Tom (3) are
selected. What seems to be happening is that SelectMultiple is
selecting 1,2,1 and 3 instead of 12 and 13.

Can anyone offer any advice? I have worked around this by using
letters instead of numbers but this work-around is less than ideal.

Thanks,

Todd

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---