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:
- null=True
- for all database fields - which, however, is very unattractive
- 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
local_shop_form.lat = lat
local_shop_form.lng = lng
local_shop_form.save()
else:
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"
local_shop_form.save()
local_shop_form.delete()
Now the delete() function is ignored.
Does anyone know a better solution?