Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: ModelMultipleChoiceField won't save data

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)
like image 863
Emile Avatar asked Jan 13 '11 19:01

Emile


1 Answers

You need to use mymodelform.save_m2m() when you do commit=False on your ModelForm because m2m relationships cannot be saved without an ID (aka save(commit=True)).

An intermediary table (m2m) doesn't have an ID to reference if the parent model isn't saved first!

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

@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()
      form.save_m2m() # save m2m after meal has id

      return HttpResponseRedirect('/meal/%d' % meal.id )
  else:
    form = MealForm()

  data = {
    'form': form,
    'add': True
  }
like image 160
Yuji 'Tomita' Tomita Avatar answered Nov 09 '22 23:11

Yuji 'Tomita' Tomita