inlineformset_factory仅保存第一行数据吗?

时间:2019-01-03 06:38:21

标签: django python-3.x django-forms inline-formset django-2.1

我正在使用 inlineformset工厂正常工作,但 formset 数据存储仅第一个值。

我正在使用 formset.js 文件添加行。

父模型数据已保存,但表单集数据仅存储了第一行信息

这是我的代码

  

forms.py

这是我的表单集代码

    SizeChartFormSet = inlineformset_factory(
        TailoringProductsMaster, SizeChart, extra=1,
        fields='__all__',
        form =SizeChartForm,
        can_delete=True,
        widgets={'SizeType': forms.TextInput(attrs={'class':"form-control col-md-7 col-xs-12",}),
                 'RequiredFabric': forms.TextInput(attrs={'class': "form-control col-md-7 col-xs-12",}),
                 },
    )
  

Views.py

这是我的查看代码

class TailoringProductsMasterCreateView(CreateView):
    model = TailoringProductsMaster
    form_class = TailoringProductsMasterForm
    success_url = reverse_lazy('home')

    def get_context_data(self, **kwargs):
        context = super(TailoringProductsMasterCreateView, self).get_context_data(**kwargs)
        if self.request.POST:
            context['operations_form'] = SizeChartFormSet(self.request.POST, self.request.FILES)

        else:
            context['operations_form'] = SizeChartFormSet()

        return context

    def post(self, request, *args, **kwargs):
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        operations_form = SizeChartFormSet(self.request.POST, self.request.FILES)


        if (form.is_valid() and operations_form.is_valid()):
                return self.form_valid(form, operations_form)
        else:
            return self.form_invalid(form, operations_form)


    def form_valid(self, form, operations_form):

        self.object = form.save()
        operations_form.instance=self.object
        operations_form.save()

        return HttpResponseRedirect(self.get_success_url())


    def form_invalid(self, form, operations_form):

        return self.render_to_response(
            self.get_context_data(form=form, operations_form=operations_form ))
  

html页面(请参见此链接以获取htmle主页-http://www.codeshare.io/arY9l6参见此html页面

                                                <tbody>
                                                     {% if operations_form.errors %}
                                {{ operations_form.errors }}
                            {% endif %}
                            {{ operations_form.non_field_erros }}

                            {% for object in operations_form %}
                            {{ object.id }}
                            <tr id="formset">
                                <td class="m-ticker">{{ object.SizeType }}</td>
                                <td>{{ object.RequiredFabric }}</td>
                                <td></td>
                            </tr>
                            {% endfor %}
                                                </tbody>

                                            </table>
{{ operations_form.management_form }}

1 个答案:

答案 0 :(得分:0)

在您的模板中,您将覆盖表格 这个你做2x

{% for object in operations_form %}
  {{ object.id }}
  <tr id="formset">
  <td class="m-ticker">{{ object.SizeType }}</td>
  <td>{{ object.RequiredFabric }}</td>
  <td></td>
  </tr>
{% endfor %}

这一次

{% for gal in operations_form %}
  {{ gal.id }}
  <tr id="formset">
  <td class="m-ticker">{{ gal.SizeType }}</td>
  <td>{{ gal.RequiredFabric }}</td>
  </tr>
{% endfor %}

但这不是处理表单集的方法。您现在在模板中所做的基本上是这样(三遍):

{% for form in formset %}
  {{ form.id }}
  {{ form.SizeType }}
  {{ form.RuiquiredFabric }}    
{% endfor %}

这将导致使用完全相同的html呈现相同的表单(您只能使用不同的变量名,但这不会更改呈现的html)

这就是为什么帖子数据中仅显示一种表单(套)的原因。

更新

html是正确的,所有数据都在request.POST中,所以它可能与您的form_valid()有关?

尝试:

operations = operations_form.save(commit=False) 
for operation in operations: 
    operation.(fill in the field reference) = self.object
    operation.save()
operations.save()