I have a django form that allows a user to select multiple options:
CARDS = (
("visa", "Visa"),
("mastercard", "MasterCard"),
)
class PaymentForm(forms.ModelForm):
credit_cards = forms.MultipleChoiceField(choices=CARDS, widget=forms.CheckboxSelectMultiple)
# ... etc.
I have the form's associated model setup as:
class Payment(models.Model):
user = models.OneToOneField(User)
credit_cards = models.CharField(choices=CARDS, max_length=100)
# ... etc.
But I'm thinking that a CharField with the choices parameter can only accept a single choice because my form never validates and I get an error like:
Value u"[u'visa']" is not a valid choice.
And it sure looks like a valid choice.
I've seen that some people get this working with a ManyToManyField on the model side (which I'd expect) but building a model just for a static list of credit card types seems overkill.
So: is there a specific model field type or different form configuration I should be using to support multiple selections from a pre-defined list of options?
Thanks.