Do not save all models in MultiModelForm

Hello, everyone,
I have a “CreateView” in which I fill several models with data using MultiModelForm (betterforms.multiform).

I have only one problem:
In my form, the user has to choose between two options - accordingly, there is always one model left in which no new instance should be created.

For example, if the user selects “Option A” - all data should be created in Model-A and Model-C - Model-B remains unaffected.

For example, if the user selects “Option B” - all data should be created in Model-B and Model-C - Model-A remains unaffected.

I implemented the following two solutions myself:

  1. null=True
  • for all database fields - which, however, is very unattractive
  1. Save instance manually
  • with a certain label, e.g. “delete” and create a CroneJob for deletion.

Then I tried this:

        if option_a:
            local_shop_form = form["LocalShopForm"].save(commit=False)
            local_shop_form.shop_id = shop_id
   = lat
            local_shop_form.lng = lng
            local_shop_form = form["LocalShopForm"].save(commit=False)
            local_shop_form.shop_id = shop_id
            local_shop_form.street_name = "delete"
            local_shop_form.house_number = 0
            local_shop_form.location = "delete"
            local_shop_form.post_code = 0
            local_shop_form.phone_number = 0
            local_shop_form.e_mail = "delete@delete.delete"

Now the delete() function is ignored.

Does anyone know a better solution?

I would not try to use any of the generic CBVs in this case. I’d write an FBV that saves the forms as appropriate.

The generic edit CBVs really are targeted to “single model” / “single form” situations. Trying to do anything else with them tends to create more work than you save.

You could probably make it work as well with a FormView subclass, but that’s about as far as I would go.

Alright, thanks for your review :slight_smile: