That would then depend (in part) on the specific form being used. Is the foreign key part of the form? If so, then it would be a field in the form in the formset, and can be initialized. (But then itâs subject to being changed by the user.) If itâs not a field in the form and it can only be set within the POST handler, then you would need to save the individual forms with commit=False to get the new objects being created and update those objects before saving them.
As i notice you are trying to save formset which tied to main form.
If you are saving formset with Product which is not saved yet you canât save formset just by using save function.
For example
#Item is main Model
item = main_form.save(commit=False)
item.position = 0 #Chengo position on attribute you need to specify before saving
item.save()
#Formsets
#item_images and item_addons are formests tied by item attribute to Main model
for item_image in item_images:
item_image = item_image.save(commit=False)
item_image.item = item
item_image.save()
for item_addon in item_addons:
item_addon = item_addon.save(commit=False)
item_addon.item = item
item_addon.save()
Welcome @OstapTelychko ! Just a side note here: When youâre posting code in the forum, surround that code between lines of three backtick - ` characters. This means youâll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.
Iâve taken the liberty of modifying your post for this, please remember to do this yourself in the future.