Наверное уже мертвый вопрос, но тем не менее, может еще что то посоветуют. Или мне самой еще это когда то понадобится, буду знать где искать.
У меня получилось так:
# forms.py
StockroomWorkerFormSet = inlineformset_factory(Stockroom, WorkerType, extra=0, can_delete=True)
StockroomGoodsFormSet = inlineformset_factory(Stockroom, GoodsType, extra=0, can_delete=True)
# views.py
class StockroomEditView(UpdateView):
model = Stockroom
temlate_name = 'stock/edit.html'
def get_success_url(self):
return self.get_object().get_absolute_url()
def get(self, request, *args, **kwargs):
self.object = self.get_object()
worker_formset = self.get_form(StockroomWorkerFormSet)
goods_formset = self.get_form(StockroomGoodsFormSet)
return self.render_to_response(self.get_context_data(
worker_formset=worker_formset, goods_formset=goods_formset))
def get(self, request, *args, **kwargs):
self.object = self.get_object()
worker_formset = self.get_form(StockroomWorkerFormSet)
goods_formset = self.get_form(StockroomGoodsFormSet)
if worker_formset.is_valid() and goods_formset.is_valid():
return self._form_valid(worker_formset, goods_formset)
else:
return self._form_invalid(worker_formset, goods_formset)
def _form_valid(self, worker, goods):
worker.save()
goods.save()
return HttpResponseRedirect(self.get_success_url())
edit.html
<form action="" method="post">
{% csrf_token %}
{{ worker_formset.management_form }}
{{ goods_formset.management_form }}
{% for form in worker_formset %}
{{ form }}
{% endfor %}
{% for form in goods_formset %}
{{ form }}
{% endfor %}
</form>
Если кто то меня слышит, на сколько правильно так делать?