开发者

Why does cleaned_data give a list encoding as a string?

开发者 https://www.devze.com 2023-01-07 21:00 出处:网络
I\'ve got a field defined like this: service_types = CharField(widget=CheckboxSelectMultiple(choices=ServiceTypes), initial=[ServiceTypes.OPEN_TRANS])

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.

0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消