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 以添加其他表单。

每个调查对象可以有一个或多个与之关联的调查区域,我希望使用相同的表单使它们可编辑。但是,我在渲染初始数据时遇到了问题。这是我的看法:

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()方法中SurveyUpdateView,我正在获取当前与该调查相关的所有区域,并且使用 PDB 我已经能够确认在我获取与该调查相关的区域(在可变区域中)时,数据似乎是正确的。以下是 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'}]

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

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

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

4

1 回答 1

1

你有没有尝试过

area_form = AreaFormSet(instance=self.object)

代替

area_form = AreaFormSet(initial=areas)

?

于 2014-05-02T16:21:24.977 回答