开发者

django: How to use inlineformset within the formwizard?

I'm displaying two separate sample projects. The first is a Contact related and shows the principle of using the formwizard. The second is an ingredients to recipes related project which shows how to use inlines within a form. I want inlines to be in my formwizard the same way they work in a normal form.

I have a formwizard multistep form working. It is based off the example here. I've changed it slightly to use modelform.

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 inlines being generated into another form. I'm doing this via inlineformset_factory in my view. This is not connected to the formwizard example above. This is an ingredients to recipes example. I'm doing this 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 get the inlines to work within my formwizard multistep form? The models.py now looks like this because I want books to be inlines to contact. I want the inlines to be on the first step of my formwizard. Then go through to step 2 and finish.

from django.db import models

# Create your model开发者_JS百科s 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)


The formwizard included in Django (below version 1.4) doesn't support formsets. Beginning with version 1.4, there will be a much better implementation (see https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/)

Back to your question, if you can't wait for the next Django release - which I assume - you could stick to django-formwizard. The last release (1.0) is api compatible to the upcoming Django formwizard.

With the new formwizard implementation you can use FormSets the same way you use normal Forms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜