开发者

Django How to work with MultipleChoiceField

form.py:

CHECKBOX_CHOICES = (
         ('Value1','Value1'),
         ('Value2','Value2'),
)

class EditProfileForm(ModelForm):
    interest = forms.MultipleChoiceField(required=False, 
                                    widget=CheckboxSelectMultiple(), 
                                    choices=CHECKBOX_CHOICES,)

    def save(self, *args, **kwargs):
        u = self.instance.user
        u.interest = self.cleane开发者_如何学编程d_data['interest']
        u.save()
        profile = super(EditProfileForm, self).save(*args,**kwargs)
        return profile

it saves in db as [u'value1', u'value2']

Now, How can I only render in my template to show as string like value1, value2 without [u' '] or is there a better way to save the value as a string?


u.interest = u','.join(self.cleaned_data['interest'])
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜