How to save a MultipleChoiceField in Django

  database, django, model, python, web

I was wondering how can I save a MultipleChoiceField to Django’s database (in form submission)?

models.py

class Model1(models.Model):
    field = models.CharField(max_length=200, null=False, blank=False, default='')

forms.py

OPTIONS = (
    ('Option 1', 'Option 1'),
    ('Option 2', 'Option 2'),
    ('Option 3', 'Option 3'),
)
class Model1Form(forms.ModelForm):
    field = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=OPTIONS)

    class Meta:
        model = Model1
        fields = '__all__'

If you need additional information let me know! Thanks for helping out.

Source: Python Questions

LEAVE A COMMENT