I am trying to figure out how to get two things figured out with Django (1.6) forms. I am looking to create a form using ModelForm with a model that has more then one ManyToManyField using through. I can seem to figure out how to get the ManyToMany items to be displayed in the form.
The one other thing I am trying to figure out is how to split up the form. For example if I wanted a form that looked like this.
Note: How its part of the Recipe Model (name, desciption) then the ManyToManyField (Fruit and items that go with it.) then another item from the Recipe Model (Fruit_notes) and then the second ManyToManyField...etc
Form Example:
Name: __________________ Description: ___________
Fruit: ___________ QTY: _________
Fruit Notes: ____________
Veg: ___________ qty: __________
veg notes:___________
models.py File
from django.db import models
class Vegetable(models.Model):
name = models.CharField(max_length=150, blank=True)
description = models.TextField(blank=True)
def __unicode__(self):
return self.name
class Fruit(models.Model):
name = models.CharField(max_length=150, blank=True)
description = models.TextField(blank=True)
def __unicode__(self):
return self.name
class Recipe(models.Model):
name = models.CharField(max_length=450, blank=True)
description = models.TextField(blank=True)
fruits = models.ManyToManyField('Fruit', through='RecipeFruit')
fruit_notes = models.TextField(blank=True)
vegetables = models.ManyToManyField('Vegetable' through='RecipeVegetable')
Vegetables_notes = models.TextField()
class RecipeVegetable(models.Model):
recipe = models.ForeignKey(Recipe)
veg = models.ForeignKey(Vegetable)
qty = models.FloatField()
class RecipeFruit(models.Model):
recipe = models.ForeignKey(Recipe)
fruit = models.ForeignKey(Fruit)
qty = models.FloatField()
forms.py File
from django import forms
from django.forms import ModelForm, Textarea
from django.forms.formsets import formset_factory
from models import Recipe, Vegetable, Fruit
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class CreateRecipeForm(forms.ModelForm):
class Meta:
model = Recipe
def __init__(self, *args, **kwargs):
super(CreateRecipeForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-RecipeForm'
self.helper.form_class = 'form-inline'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_survey'
self.helper.add_input(Submit('submit', 'Submit'))
I did come across this question. What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django?
Looks like a great step toward figuring this out but I am having a hard time grasping on how to bring in two items. (In example, fruit and Vegetable)
If someone could point my in the right direction on this and provide a little example, it would be so greatly appreciated. When looking at the django docs, I don't really see anything that spells this out.