How-to use SelectMultiple widget with CommaSeparatedIntegerField ?

470 views
Skip to first unread message

nono

unread,
Aug 6, 2009, 8:21:53 AM8/6/09
to Django users
Hi,

my model uses CommaSeparatedIntegerField to store a list of integer. I
want my users to use SelectMultiple widget to create/update this field
so I put a ChoiceField with widget=SelectMultiple in my form. My
problem is that all I can get from this is a list of values where I
expect comma separated values.
Do you have a solution for me ?

regards

nono

unread,
Aug 6, 2009, 10:35:48 AM8/6/09
to Django users
I was reading my post and though it was not very clear

My model uses CommaSeparatedIntegerField to store some integer values.
I want my users to use SelectMultiple widget to create/update those
integer so I put a ChoiceField with widget=SelectMultiple in my form.
My problem is that all I can get from this is a list of values (for
example [u'2', u'3']) where I expect comma separated values ("2, 3").

I hope this is a better explanation and somebody could help me

Malcolm Tredinnick

unread,
Aug 6, 2009, 9:52:47 PM8/6/09
to django...@googlegroups.com
On Thu, 2009-08-06 at 07:35 -0700, nono wrote:
> I was reading my post and though it was not very clear
>
> My model uses CommaSeparatedIntegerField to store some integer values.
> I want my users to use SelectMultiple widget to create/update those
> integer so I put a ChoiceField with widget=SelectMultiple in my form.
> My problem is that all I can get from this is a list of values (for
> example [u'2', u'3']) where I expect comma separated values ("2, 3").
>
> I hope this is a better explanation and somebody could help me

If you want to normalise the values that are submitted to something
other than what happens automatically, write a clean_<fieldname>()
method for that form field. Have a look at

http://docs.djangoproject.com/en/dev/ref/forms/validation/

for details.

Regards,
Malcolm


nono

unread,
Aug 11, 2009, 11:02:07 AM8/11/09
to Django users
I wrote a clean_csi() method and I call it in each clean_<fieldname>()
method passing the field name and
and it's working now

Here is the code

def clean_csi(self, field):
data = self.cleaned_data[field]
data.sort()
csi_list = []
first_element = True
for element in data:
if not first_element:
csi_list.append(",")
else:
first_element = False
csi_list.append(str(element))
return "".join(csi_list)

def clean_hour(self):
return self.clean_csi('hour')

Thank you for your help !
Reply all
Reply to author
Forward
0 new messages