I've got a field defined like this:
service_types = CharField(widget=CheckboxSelectMultiple(choices=ServiceTypes), initial=[ServiceTypes.OPEN_TRANS])
I want to "clean" it to return a single integer (the choices are encoded as power-of-2 flags):
def clean_service_types(self):
data = self.cleaned_data['service_types']
return sum(map(int, data))
But it throws me an error, "cannot convert [
to int". turns out data
is:
u"[u'1', u'4', u'32']"
...oh, just occured to me that this is probably because it's a CharField
. However, when I change it to a MultipleChoice开发者_运维知识库Field
nothing gets rendered. How do I fix this?
Nevermind. Have to move the choices
out of the widget constructor:
MultipleChoiceField(widget=CheckboxSelectMultiple, choices=ServiceTypes, initial=[ServiceTypes.OPEN_TRANS])
I'll leave this question here in case any one else is scratching their head.
精彩评论