My modelform has a field with a ModelMultipleChoiceField that has a queryset to a Category object. The problem is the form submits, but the Category data doesn't save. Thoughts?
Many thanks!
Form:
class MealForm(forms.ModelForm):
class Meta:
model = Meal
fields = ('category','date','time')
category = forms.ModelMultipleChoiceField(
label=_("Food style"),
queryset=Category.objects.all(),
required=True)
View
@login_required
def new_meal(request, template_name="meal/newmeal.html"):
if request.method == 'POST':
form = MealForm(request.POST)
form.mom = request.user
if form.is_valid():
meal = form.save(commit=False)
meal.mom = request.user
meal.save()
return HttpResponseRedirect('/meal/%d' % meal.id )
else:
form = MealForm()
data = {
'form': form,
'add': True
}
Models:
class Category(models.Model):
name = models.CharField(default='',max_length=100)
num_tags = models.IntegerField(default=0)
def __unicode__(self):
return "%s" % self.name
class Meta:
ordering = ['name']
class Meal(models.Model):
category = models.ManyToManyField(Category)