Плагин импорта-экспорта django возвращает только 1 строку в excel

У меня есть набор форм в моем DetailView, где я позволяю пользователю вводить количество единиц в форму и после отправки он возвращает файл excel с экспортированными данными.

Моя проблема в том, что я получаю только 1 строку в моем экспортированном файле Excel, и я понятия не имею почему.

Не могли бы вы взглянуть на мой код и сообщить мне, в чем проблема? Я считаю, что что-то не так с моим циклом в методе form_valid, но я не уверен, что именно.

Edit: Самое интересное, что когда я печатаю qs, я получаю все компоненты, но в Excel я вижу только 1:

enter image description here

views.py

class ProductDetailView(FormMixin, DetailView):
    template_name = 'hubble/ProductDetailView.html'
    model = Product
    form_class = CalculatorFormsetProduct
    
    def get_context_data(self, **kwargs):
        context = super(ProductDetailView, self).get_context_data(**kwargs)
        get_components = CostCalculator.objects.filter(related_product__slug=self.kwargs['slug'])
        form = CalculatorFormsetProduct(initial=[{
            'author': self.request.user.email,
            'related_product': x.related_product,
            'related_component': x.id,
            'created': timezone.now,
            'number_of_units': 0
            } for x in get_components])
            
        context['ce_form'] = form
        return context  

    def get_success_url(self):
        return reverse('product', kwargs={'slug': self.kwargs['slug']})

    def post(self, request, *args, **kwargs):
        form = self.get_form()
        self.object = self.get_object()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def form_valid(self, form):
        for f in form:
            related_product = f.cleaned_data.get('related_product')
            created = f.cleaned_data.get('created')
            f.save()
            qs = CERequest.objects.filter(related_product__title=related_product, author=self.request.user, created=created)
            dataset = CERequestResource().export(qs)
            response = HttpResponse(dataset.xlsx, content_type="xlsx")
            response['Content-Disposition'] = 'attachment; filename=cost_estimation.xlsx'
        return response

resources.py

class CERequestResource(resources.ModelResource):
    related_component__service = Field(attribute='related_component__service', column_name='Service')
    related_product__title = Field(attribute='related_product__title', column_name='Product')
    related_component__component_name = Field(attribute='related_component__component_name', column_name='Component')
    related_component__task_description = Field(attribute='related_component__task_description', column_name='Task Description')
    related_component__position = Field(attribute='related_component__position', column_name='Who - (Position)')
    number_of_units = Field(attribute='number_of_units', column_name='# of Units')
    related_component__margin = Field(attribute='related_component__margin', column_name='Margin')
    total_price = Field(attribute="total_price", column_name='Price(€)')
    
    model = CERequest
    fields = ('id', 'related_component', 'related_product', 'number_of_units', 'total_price', 'margin')

html файл

<form method="post">
{% csrf_token %}

<table class="table">
    {{ ce_form.management_form }}
    <thead>
        <th>Product</th>
        <th>Component</th>
        <th>Number of units</th>
    </thead>
    <tbody>
        {% for form in ce_form.forms %}
        <tr class="{% cycle 'row1' 'row2' %} formset_row">
            {% for field in form.visible_fields %}
            <td>
                {% if forloop.first %}
                    {% for hidden in form.hidden_fields %}
                        {{ hidden }}
                    {% endfor %}
                {% endif %}
                {{ field.errors.as_ul }}
                {{ field }}
            </td>
            {% endfor %}
        </tr>
        {% endfor %}
    </tbody>
</table>
<button type="submit">Save</button>
Вернуться на верх