首页 文章

Django UpdateView的内联formset

提问于
浏览
1

我在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 ,它基本上只是限制用户的查询集并自动设置表示用户的字段 .

我遇到的问题是渲染初始数据 . 在 SurveyUpdateViewget() 方法中,我_2980260已经能够确认在我获取与此调查相关的区域(在可变区域中),数据似乎是正确的 . 以下是8个项目的外观:

[{'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'}]

但是,在实例化 AreaFormSet 时,将该数据作为 initial 的值传递不会正确呈现数据 . 如果在 AreaFormSet 的定义中,我设置了 extra 的值,我得到了渲染区域的数量(如果未定义,则显示三个,我认为是 extra 的默认值) . 我想看到的行为是每个现有区域以自己的形式呈现 .

再次使用PDB,如果我在使用 area_form.as_table() 设置之后转储area_form的值,我只获得 extra 中设置的表单数量,因此问题似乎与传递初始数据有关 .

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

1 回答

  • 1

    你有没有尝试过

    area_form = AreaFormSet(instance=self.object)
    

    代替

    area_form = AreaFormSet(initial=areas)
    

相关问题