在基于 Django Class 的视图中使用 modelformset_factory

Using modelformset_factory in a Django Class Based View

我正在构建一个视图,可以让我同时更新多个对象的多个字段。我正在使用 ModelFormSet & modelformset_factory.

模板将是一个 table 表单,对象名称位于字段的左侧(见下图)。

我找到了 this 示例,但我仍然不知道如何实现基于 class 的视图和模板。

我的表格集

class BaseFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(BaseFormSet, self).__init__(*args, **kwargs)
        self.queryset = Reference.objects.filter(
            start__isnull=True)


ReferenceFormSet = modelformset_factory(
    Reference,
    fields=('start', 'end'),
    formset=BaseFormSet,
    extra=0)

我的看法

class ReferenceFormSetView(LoginRequiredMixin, SuperuserRequiredMixin, FormView):
    model = Reference
    form_class = ReferenceFormSet
    template_name = "references/references_form.html"

    def form_valid(self, form):
        for sub_form in form:
            if sub_form.has_changed():
                sub_form.save()

        return super(ReferenceFormSetView, self).form_valid(form)

我的模板

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

{% block content %}
<div class="container">
    <h1>{{ headline }}</h1>
    <div class="row">
        <form action="" method="post">
            {% crispy form %}
            <div class="">
                <input type="submit" value="Submit" />
            </div>
        </form>
    </div>
</div>
{% endblock content %}

问题

我使用名为 django-extra-views 的软件包找到了解决方案。

有一个名为 ModelFormSetView 的 class,它完全符合我的要求。这是我的实现(简化)供其他人使用 -

我的观点

class ReferenceFormSetView(ModelFormSetView):
    model = Reference
    template_name = "references/references_form.html"
    fields = ['start', 'end']
    extra = 0

    def get_queryset(self):
        return self.model.objects.all()

    def get_success_url(self):
        return reverse('references:formset')

    def formset_valid(self, formset):
        """
        If the formset is valid redirect to the supplied URL
        """
        messages.success(self.request, "Updated")
        return HttpResponseRedirect(self.get_success_url())

    def formset_invalid(self, formset):
        """
        If the formset is invalid, re-render the context data with the
        data-filled formset and errors.
        """
        messages.error(self.request, "Error dummy")
        return self.render_to_response(self.get_context_data(formset=formset))

我的模板

<form class="" method="post">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}
        <div class="">
            {% for field in form %}
                {{ field }}
            {% endfor %}
        </div>
    {% endfor %}
    <button type="submit" class="btn btn-primary">Save</button>
</form>