Django: save() prohibited to prevent data loss due to unsaved related object 'predictions'

True for standard initialisation, but for the initialisation of FormSets Django iterates over the num of forms and therefore it needs a list of dicts.

See here: https://docs.djangoproject.com/en/4.2/topics/forms/formsets/#using-initial-data-with-a-formset

in my final code, this would look like such:

formset = metaFormset(data, initial=[dict(ref=obj) for x in range(int(data['form-TOTAL_FORMS']))])

No, that’s not correct - as far as this specific issue is concerned. You are not initializing the individual forms, the inline formset class is.

An inline formset uses one instance of the parent object to identify the foreign key reference.

Quoting directly from the example in the docs I referenced:

>>> formset = BookFormSet(instance=author)

Yes, but I do not talk about Inline FormSets :wink:

I’m talking about ModelForm FormSets.
https://docs.djangoproject.com/en/4.2/topics/forms/formsets/#using-initial-data-with-a-formset

I can’t reconcile this:

With this:

So are you using an inline formset (as you should in this situation), or are you not? (And if you’re not, why not?)

the line right below what you quoted…

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.