Django: How to use inlineformset inside formwizard?

I show two separate example projects. The first is contact-related and shows how to use the formwizard. The second is the ingredients for a recipe project that shows how to use inline strings in a form. I want inline strings in my formwizard just as they work in normal form.

I have a multi-format formwizard form. It is based on an example here . I modified it a bit to use the model.

models.py

from django.db import models # Create your models here. class Contact(models.Model): subject = models.CharField(max_length=50) sender = models.EmailField() def __unicode__(self): return self.subject class Contact2(models.Model): message = models.TextField(max_length=500) def __unicode__(self): return self.message 

forms.py

 class ContactForm1(forms.ModelForm): class Meta: model = Contact class ContactForm2(forms.ModelForm): class Meta: model = Contact2 class ContactWizard(FormWizard): @property def __name__(self): return self.__class__.__name__ def done(self, request, form_list): # do_something_with_the_form_data(form_list) return HttpResponseRedirect('/done/') 

urls.py

 (r'^contact/$', ContactWizard([ContactForm1, ContactForm2])), 

Separately, I have lines embedded in another form. I do this through inlineformset_factory in my opinion. This is not related to the above form example. . This is an example of recipe ingredients. I do it like:

views.py

 def add(request): IngredientFormSet = inlineformset_factory(Recipe, Ingredient, fk_name="recipe", formfield_callback=curry(ingredient_form_callback, None)) if request.method == 'POST': form = RecipeForm(request.POST) formset = IngredientFormSet(request.POST) if form.is_valid() and formset.is_valid(): recipe = form.save() formset = IngredientFormSet(request.POST, instance=recipe) formset.save() return redirect("/edit/%s" % recipe.id) else: form = RecipeForm() formset = IngredientFormSet() return render_to_response("recipes_add.html", {"form":form, "formsets":formset}, context_instance=RequestContext(request)) 

recipes_add.html

 <form method="post"> {% csrf_token %} <table> {{ form }} </table> <hr> <h3>Ingredients</h3> <div class="inline-group"> <div class="tabular inline-related last-related"> {{ formsets.management_form }} {% for formset in formsets.forms %} <table> {{ formset }} </table> {% endfor %} </div> </div> <p class="success tools"><a href="#" class="add">Add another row</a></p> <input type="submit" value="Add"> </form> 

How can I make inline lines work in my multi-line formwizard form? models.py now looks like this because I want books to be embedded in contact . I want the lines to be in the first step of my viewfinder. Then go to step 2 and finish.

 from django.db import models # Create your models here. class Contact(models.Model): subject = models.CharField(max_length=50) sender = models.EmailField() def __unicode__(self): return self.subject class Contact2(models.Model): message = models.TextField(max_length=500) def __unicode__(self): return self.message class Book(models.Model): author = models.ForeignKey(Contact) title = models.CharField(max_length=100) 
+4
source share
1 answer

The form module included in Django (below version 1.4) does not support forms. Starting with version 1.4, the implementation will be significantly improved (see https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/ )

Let's get back to your question if you can't wait for the next release of Django, which I assume you can stick with the django-formwizard. The latest version (1.0) is api compatible with the upcoming Django formwizard.

With the new implementation of formwizard, you can use FormSets the same way you use regular forms.

+3
source

All Articles