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

**URL:** <https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730>\
**Category:** Forms & APIs\
**Created:** [December 19, 2022, 5:35pm UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730 "2022-12-19T17:35:03Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![ToM-Korn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tom-korn/32/13480_2.png) [@ToM-Korn](https://forum.djangoproject.com/u/ToM-Korn)\
**Post date:** [November 20, 2023, 7:59am UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/21 "2023-11-20T07:59:21Z")

</div>

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](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:

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 20, 2023, 11:57am UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/22 "2023-11-20T11:57:03Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![ToM-Korn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tom-korn/32/13480_2.png) [@ToM-Korn](https://forum.djangoproject.com/u/ToM-Korn)\
**Post date:** [November 20, 2023, 12:57pm UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/23 "2023-11-20T12:57:17Z")

</div>

Yes, but I do not talk about Inline FormSets 😉

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 20, 2023, 1:23pm UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/24 "2023-11-20T13:23:31Z")

</div>

I can’t reconcile this:

> [@ToM-Korn](#):
>
> Yes, but I do not talk about Inline FormSets

With this:

> [@ToM-Korn](#):
>
> Thanks, that specific document was the solution.  
> I solved my Problem with the section “Using an inline formset in a view”

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

---

<div class="post-metadata">

**Author:** ![ToM-Korn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tom-korn/32/13480_2.png) [@ToM-Korn](https://forum.djangoproject.com/u/ToM-Korn)\
**Post date:** [November 20, 2023, 3:52pm UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/25 "2023-11-20T15:52:15Z")

</div>

> [@ToM-Korn](#):
>
> Thanks, that specific document was the solution.  
> I solved my Problem with the section “Using an inline formset in a view”
> 
> But If i use a ModelForm FormSet with a referenced object.  
> How would I set the object when I receive the form data?

the line right below what you quoted…

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 20, 2023, 3:58pm UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/26 "2023-11-20T15:58:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![OstapTelychko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ostaptelychko/32/18122_2.png) [@OstapTelychko](https://forum.djangoproject.com/u/OstapTelychko)\
**Post date:** [January 2, 2024, 5:04pm UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/27 "2024-01-02T17:04:15Z")

</div>

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

```auto
#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()

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 2, 2024, 5:07pm UTC](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730/28 "2024-01-02T17:07:28Z")

</div>

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.

[Previous page](https://forum.djangoproject.com/t/django-save-prohibited-to-prevent-data-loss-due-to-unsaved-related-object-predictions/17730.md?page=1)
