使用inline_formset

时间:2019-12-05 01:31:34

标签: django formset

在我的Web应用程序中,我设法创建了一个运行良好的inline_formset。 现在的问题是,当我有200行时,每个具有外键的字段都会被调用200次以生成下拉菜单。 我尝试使用预取,但查询仍然重复,请问如何只进行一次查询以获取下拉菜单列表?

forms.py

class IssueForm(forms.ModelForm):
    class Meta:
        model=Issue
        fields=('__all__')
IssueFormset=inlineformset_factory(IssuesList, Issue, form=IssueForm,extra=0)
in views.py

class IssuesListUpdateView(UpdateView):
    model=IssuesList
    fields='__all__'

    # ovewrite get_context_data to add formset as an additionnal parameter
    def get_context_data(self, **kwargs):
        context = super(IssuesListUpdateView, self).get_context_data(**kwargs)
        if self.request.POST:
            # if Save All issues button submitted
            if 'save_all' in self.request.POST.keys():
                formset = IssueFormset(self.request.POST, instance=self.object)
                # sending formset to the template
                context['issues_formset'] = formset
                # save in the DB (saves only what has changed)
                #https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#saving-objects-in-the-formset
                # if formset.is_valid():
                formset.save()
                # re-update context with new formset, this is need to refresh table in case of deleting an object
                # without this, issue is deleted but still visible in the table 
                context['issues_formset'] = IssueFormset(instance=self.object)
        else:
            # sending formset to the template
            issues_formset=IssueFormset(instance=self.object)
            for form in issues_formset:    
               form.fields["si"].queryset=Si.objects.prefetch_related('si_issues').\ all().filter(product__name='test')
            context['issues_formset'] = issues_formset
        return context

在模板中:

    <form method="post">{% csrf_token %}
    <!-- Below line is important to avoid django exception: 
    [django 'ManagementForm data is missing or has been tampered with] -->
        {{ issues_formset.management_form }}
        <input name="save_all" type="submit" value="Save All Issues"> 
<div class="">
        <table id="formset" class="form" border="1">
                {% for form in issues_formset.forms %}
                  {% if forloop.first %}
                  <thead><tr>
                    {% for field in form.visible_fields %}
                    <th>{{ field.label|capfirst }}</th>
                    {% endfor %}
                  </tr></thead>
                  {% endif %}
                  <tr>
                  {% for field in form.visible_fields %}
                    <td>
                    {# Include the hidden fields in the form #}
                    {% if forloop.first %}
                      {% for hidden in form.hidden_fields %}
                      {{ hidden }}
                      {% endfor %}
                    {% endif %}
                      {{ field.errors.as_ul }}
                      {{ field }}
                    </td>
                  {% endfor %}
                  </tr>
                {% endfor %}
                </table>  

</div>
</form>

0 个答案:

没有答案