如何在窗体向导中使用ModelFormSet

时间:2014-11-21 21:47:38

标签: python django forms django-forms django-formwizard

关于这个主题的Django文档没有很好的文档记录。实际上,他们在文档中唯一的参考是这一段:

  

如何使用ModelForm和ModelFormSet

     
    

WizardView.instance_dict。的     WizardView支持ModelForms和ModelFormSet。除了initial_dict之外,as_view()>方法还接受一个instance_dict参数,该参数应该包含基于> ModelForm的步骤的模型实例和基于ModelFormSet的步骤的查询集。

  

我没有找到关于如何使用它的任何好的和明确的例子。有人可以帮我这个吗?

具体做法是:

  • 在forms.py中做什么?
  • 如果我只在表单的某些步骤上需要ModelFormSet,而不是所有步骤,该怎么办?
  • 我需要在views.py和模板中做什么?

举一个用例和我正在研究的小项目作为一个例子,我分享了我的代码:

  • 使用案例:
    • 用户想要以多步形式注册,在第一步中他会介绍他的姓名和姓氏。
  • 在第二步中他介绍了他的护照号码和酒店登记,他还想登记他的儿子和妻子,他们和他一起去这家酒店(这里我想使用HotelRegistration模型中的modelformset)。 / LI>
  • 在第三步中他输入了他的航班信息。如果表单有效并保存在数据库中,则会收到确认消息。

这是我的代码:

models.py

class Event(models.Model):

    name = models.CharField(max_length=100)
    date_from = models.DateField(auto_now=False)
    date_to = models.DateField(auto_now=False)
    description = models.TextField()

    def __unicode__(self):
       return self.name


class Hotel(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name


class HotelRegistration(models.Model):
    pax_first_name = models.CharField(max_length=50)
    pax_last_name = models.CharField(max_length=50)
    hotel = models.ForeignKey(Hotel)

    def __unicode__(self):
         return self.pax_first_name


class Registration(models.Model):

    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    passport = models.CharField(max_length=15)
    city_origin = models.CharField(max_length=50)
    flight_date_from = models.DateField(auto_now=False)
    flight_date_to = models.DateField(auto_now=False)
    require_transfer = models.BooleanField(default=None)
    event = models.ForeignKey(Event)
    hotel_registration = models.ForeignKey(HotelRegistration, blank=True, null=True)

    def __unicode__(self):
        return self.first_name

forms.py

from django import forms

from .models import Registration

class FormStep1(forms.ModelForm):
    class Meta:
        model = Registration
        fields = ['first_name', 'last_name']
        widgets = {
            'first_name': forms.TextInput(attrs={'placeholder': 'Nombre de la persona que   esta reservando', 'label_tag': 'Nombre'}),
        'last_name': forms.TextInput(attrs={'placeholder': 'Apellido'})
    }

class FormStep2(forms.ModelForm):
    class Meta:
        model = Registration
        fields = ['passport', 'hotel_registration']
        exclude = ('first_name', 'last_name', 'event' , 'city_origin', 'flight_date_from', 'flight_date_to', 'require_transfer')
        widgets = {
             'passport': forms.NumberInput(attrs={'placeholder':'Escriba su pasaporte'})
        }

class FormStep3(forms.ModelForm):
    class Meta:
        model = Registration
        fields = ['city_origin', 'flight_date_from', 'flight_date_to', 'require_transfer', 'event']
        exclude = ('first_name', 'last_name', 'hotel_registration')
        widgets = {
            'city_origin': forms.TextInput(attrs={'placeholder':'Ciudad desde donde esta viajando'}),
            'flight_date_from': forms.DateInput(format=('%d-%m-%Y'), attrs={'class':'myDateClass', 'placeholder':'Select a date'}),
            'flight_date_to': forms.DateInput(format=('%d-%m-%Y'), attrs={'class':'myDateClass', 'placeholder':'Select a date'}),
            'require_transfer': forms.Select(),
            'event': forms.Select()
    }

views.py

from django.shortcuts import render
from django.contrib.formtools.wizard.views import SessionWizardView
from django.http import HttpResponseRedirect
from django.views.generic import TemplateView
from django.forms.models import inlineformset_factory

from .models import Registration, HotelRegistration
from .forms import FormStep1, FormStep2, FormStep3


FORMS = [
    ("step1", FormStep1),
        ("step2", FormStep2),
        ("step3", FormStep3)
]

TEMPLATES = {
    "step1" : "wizard/step1.html",
    "step2" : "wizard/step2.html",
    "step3" : "wizard/step3.html"
}


class TestFormWizard(SessionWizardView):

    instance = None

def get_form_instance(self, step):
    if self.instance is None:
        self.instance = Registration()
    return self.instance


def get_form(self, step=None, data=None, files=None):
    form = super(TestFormWizard, self).get_form(step, data, files)
    HotelRegistFormSet = inlineformset_factory(HotelRegistration, Registration, can_delete=True, extra=1)

    # determine the step if not given
    if step is None:
        step = self.steps.current

    if step == '2':
        hotel_registration_formset = HotelRegistFormSet(self.steps.current, self.steps.files, prefix="step2")
    return form


def get_template_names(self):
    return [TEMPLATES[self.steps.current]]

def done(self, form_list, **kwargs):
    self.instance.save()
    return HttpResponseRedirect('/register/confirmation')


class ConfirmationView(TemplateView):
    template_name = 'wizard/confirmation.html'

模板

   {% extends "base.html" %}
   {% load i18n %}


   {% block content %}
   <p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
   <form action="" method="post">{% csrf_token %}
   <table>
        {{ wizard.management_form }}
             {% if wizard.form.forms %}
        {{ wizard.form.management_form }}
         {% for form in wizard.form.forms %}
             {{ form }}
         {% endfor %}
     {% else %}
         {{ wizard.form }}
     {% endif %}
    </table>
     {% if wizard.steps.prev %}
     <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans   "first step" %}</button>
    <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
    {% endif %}
    <input type="submit" value="{% trans "submit" %}"/>
    </form>
    {% endblock %}

1 个答案:

答案 0 :(得分:4)

在forms.py?

中可以做些什么

覆盖SessionWizardView的get_form_instance方法。这是FormWizard用于确定是否使用模型表单

的模型实例的方法

<强> WizardView.get_form_instance(步骤) 仅当ModelForm用作步骤的表单时,才会调用此方法。 返回一个Model对象,该对象将在为步骤步骤实例化ModelForm时作为实例参数传递。如果在初始化表单向导时未提供实例对象,则将返回None。

这可以在SessionWizardView实现中的每个步骤中有条件地完成。我不明白你想做什么就能给你一个确切的例子,所以这里有一个更通用的例子。

def get_form_instance(self, step):
    if step == u'3':
        past_data =  self.get_cleaned_data_for_step(u'2')
        hotel_name = past_data['hotel_field']
        hotel = Hotel.objects.get(name=hotel_name)
        return hotel #do NOT set self.instance, just return the model instance you want
    return self.instance_dict.get(step, None) # the default implementation

如果我只在表单的某些步骤上需要ModelFormSet,而不是在所有步骤中,该怎么办?

见上文;使用'if step ==(form / step name)'表达式来确定每一步发生的事情。

我需要在views.py和模板中做什么?

使用ModelForm并传递模型对象实例将设置初始表单值。你需要更多吗?

希望这能显示FormWizard中预期的结构。我使用的Django比其他任何部分都要多,FormWizard需要一个非常特殊的结构,并且是一个单一的类。

相关问题