开发者

Django How to work with MultipleChoiceField

开发者 https://www.devze.com 2023-03-19 08:04 出处:网络
form.py: CHECKBOX_CHOICES = ( (\'Value1\',\'Value1\'), (\'Value2\',\'Value2\'), ) class EditProfileForm(ModelForm):

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

精彩评论

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