Django UpdateView的内联formset

时间:2013-11-04 09:55:52

标签: python django django-generic-views inline-formset

我的Django应用程序中有以下表单:

class SurveyAreaForm(ModelForm):
    class Media(object):
        js = formset_media_js

    class Meta:
        model = SurveyArea
        exclude = ['survey', ]
        widgets = {
            'action_by': CustomDateInput(),
        }


AreaFormSet = inlineformset_factory(Survey, SurveyArea, form=SurveyAreaForm)

class SurveyForm(ModelForm):
    class Meta:
        model = Survey
        exclude = ['tech', 'operator', ]
        widgets = {
            'date': CustomDateInput(),
            'client': FilteredJQMRadioSelectWithAdd(),
            'assessorSignature': SignatureInput(attrs={'id': 'assessorSignature'}),
        }

    def __init__(self, *args, **kwargs):
        super(SurveyForm, self).__init__(*args, **kwargs)
        self.fields['client'].empty_label = None

我正在使用https://pypi.python.org/pypi/django-formset-js/0.3.0来提供JavaScript以添加其他表单。

每个Survey对象都可以有一个或多个与之关联的SurveyAreas,我想使用相同的表单对它们进行编辑。但是,我遇到了渲染初始数据的问题。这是我的观点:

class SurveyUpdateView(SurveyValidateMixin, UpdateViewWithTech):
    model = Survey
    form_class = SurveyForm
    success_url="/forms/survey/updated/"
    template_name="my_django_app/survey_update.html"

    def get(self, request, *args, **kwargs):
        """
        Handles GET requests and instantiates blank version of the form
        and its inline formsets.
        """
        self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)

        # Get areas
        areas = SurveyArea.objects.filter(survey=self.object).order_by('name').values()

        # Render form
        area_form = AreaFormSet(initial=areas)
        return self.render_to_response(
            self.get_context_data(form=form,
                                area_form = area_form))

    def post(self, request, *args, **kwargs):
        """
        Handles POST requests, instantiating a form instance and its inline
        formsets with the passed POST variables and them checking them for
        validity.
        """
        self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        area_form = AreaFormSet(self.request.POST)
        if (form.is_valid() and area_form.is_valid()):
            return self.form_valid(form, area_form)
        else:
            return self.form_invalid(form, area_form)

我还有SurveyCreateView,工作正常,使用SurveyValidateMixin在这些视图之间共享验证。 SurveyUpdateView也继承自UpdateViewWithTech,它基本上只是限制用户的查询集,并自动设置代表用户的字段。

我遇到的问题是渲染初始数据。在get()的{​​{1}}方法中,我正在获取当前与该调查相关联的所有区域,并且使用PDB我已经能够在我获取与之相关的区域时确认这项调查(在可变区域),数据似乎是正确的。以下是8个项目的外观:

SurveyUpdateView

但是,在实例化[{'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 21L, 'action_taken': True, 'name': u'A'}, {'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 19L, 'action_taken': True, 'name': u'A'}, {'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 29L, 'action_taken': True, 'name': u'A'}, {'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 30L, 'action_taken': True, 'name': u'A'}, {'description': u'B', 'photo': u'', 'action_required': u'B', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 22L, 'action_taken': True, 'name': u'B'}, {'description': u'B', 'photo': u'', 'action_required': u'B', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 20L, 'action_taken': True, 'name': u'B'}, {'description': u'B', 'photo': u'', 'action_required': u'B', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 31L, 'action_taken': True, 'name': u'B'}, {'description': u'C', 'photo': u'', 'action_required': u'C', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 23L, 'action_taken': True, 'name': u'C'}, {'description': u'X', 'photo': u'', 'action_required': u'X', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 24L, 'action_taken': True, 'name': u'X'}, {'description': u'Y', 'photo': u'', 'action_required': u'Y', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 25L, 'action_taken': True, 'name': u'Y'}, {'description': u'Z', 'photo': u'', 'action_required': u'Z', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 26L, 'action_taken': True, 'name': u'Z'}] 时,将该数据作为initial的值传递不会正确呈现数据。如果在AreaFormSet的定义中,我设置了AreaFormSet的值,我会获得渲染的区域数量(如果未定义,则显示三个,我相信它是默认值extra )。我想看到的行为是每个现有区域以自己的形式呈现。

再次使用PDB,如果我在使用extra设置之后转储了area_form的值,我只得到area_form.as_table()中设置的表单数量,所以问题似乎是通过初始数据。

我是否正确地通过了extra的值?我知道initial的值应该是一个字典列表,它看起来对我来说是正确的,但我没有得到正确数量的区域渲染。

1 个答案:

答案 0 :(得分:1)

你试过吗

area_form = AreaFormSet(instance=self.object)

而不是

area_form = AreaFormSet(initial=areas)