takz
takz

Reputation: 23

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.cleaned_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?

Upvotes: 2

Views: 5290

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

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

Upvotes: 1

Related Questions