[Answer]-Saving Django Formsets

1👍

Have you considered using django-extra-views? It contains a quick and easy CBV for dealing with InlineFormSets.

In addition to an InlineFormSetView and GenericInlineFormSetView, they’ve also got a CreateWithInlinesView that seems to be just what you want. Relevant docs here.

Example:

from extra_views import InlineFormSet, CreateWithInlinesView, UpdateWithInlinesView,
from extra_views.generic import GenericInlineFormSet

from tool_cal.models import ToolCalibration, SerialNumber, PartNumber    


class SerialNumberInline(InlineFormSet):
    model = SerialNumber


class PartNumberInline(GenericInlineFormSet):
    model = PartNumber


class ToolCreateView(CreateWithInlinesView):
    model = ToolCalibration
    inlines = [SerialNumberInline, PartNumberInline]

    def get_success_url(self):
        return self.object.get_absolute_url()

Even if you don’t want to use them, you could dig through the code and see how they handle it.

Leave a comment