I am using jquery chosen. I’ve added null=True and blank=True on ManyToMany and ForeignKey. But then why I am getting validation error on ManyToManyField in template? I tried to submit the form without filling the ManyToManyField and ForeignKey in admin and it works but not in tempalte. Will you please help? thanks
ManyToManyError – Enter a list of values.
Model
class Movie(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(unique=True, max_length=100)
director = models.ManyToManyField(Director, null=True, blank=True)
writer = models.ManyToManyField(Writer, null=True, blank=True)
producer = models.ManyToManyField(Producer, null=True, blank=True)
starring = models.ManyToManyField(Starring, null=True, blank=True)
screenplay = models.ManyToManyField(Screenplay, null=True, blank=True)
editing = models.ForeignKey(Editing, null=True, blank=True)
music = models.ForeignKey(Music, null=True, blank=True)
studio = models.ForeignKey(Studio, null=True, blank=True)
image = models.CharField(max_length=200, null=True, blank=True)
MovieForm
class MovieForm(ModelForm):
class Meta:
model = Movie
exclude = ('slug', 'image')
widgets = {
'director': Select(attrs={'multiple class': 'chzn-select'}),
'starring': Select(attrs={'multiple class': 'chzn-select'}),
'producer': Select(attrs={'class': 'chzn-select'}),
'writer': Select(attrs={'class': 'chzn-select'}),
'studio': Select(attrs={'class': 'chzn-select'}),
'editing': Select(attrs={'class': 'chzn-select'}),
'screenplay': Select(attrs={'class': 'chzn-select'}),
'music': Select(attrs={'class': 'chzn-select'}),
}
The error message “Enter a list of values” implies the value returned from the form is not a instance of list or tuple, I think the problem is you use
Selectwidget, which return a single value(” if empty), maybe you should useSelectMultiple:Or you could manually convert the value to list/tuple before form clean.