# check for deletion in save\_related (admin.py)

**URL:** <https://forum.djangoproject.com/t/check-for-deletion-in-save-related-admin-py/26766>\
**Category:** Forms & APIs\
**Created:** [January 9, 2024, 8:14am UTC](https://forum.djangoproject.com/t/check-for-deletion-in-save-related-admin-py/26766 "2024-01-09T08:14:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjipkevdh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tjipkevdh/32/18254_2.png) [@tjipkevdh](https://forum.djangoproject.com/u/tjipkevdh)\
**Post date:** [January 9, 2024, 8:14am UTC](https://forum.djangoproject.com/t/check-for-deletion-in-save-related-admin-py/26766/1 "2024-01-09T08:14:53Z")

</div>

Hey everyone, I have probably a very simple question, that i haven’t been able to figure out for a while.  
Our django application had a hard crash whenever someone would delete anything in the administration panel. After narrowing it down to the following piece of code i found out what the problem was:

```auto
def save_related(self, request, form, formsets, change):
    for single_form in formsets:
        if single_form.model == Malfunction and single_form.has_changed():
            new_instance = single_form.save(commit=False)[0]

```

This is part of a class that extends ModelAdmin, and i omitted what comes after it for brevity. What this part is supposed to do, is when a related object has been changed, and the related object is of type Malfunction, it gets the changed instance of it.  
My question is now however, if such an object has been deleted instead of changed, how can i check for that? Because if it has been deleted, that form.save() call will fail.

---

<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 9, 2024, 1:13pm UTC](https://forum.djangoproject.com/t/check-for-deletion-in-save-related-admin-py/26766/2 "2024-01-09T13:13:59Z")

</div>

See the section of the formset docs on [`can_delete`](https://docs.djangoproject.com/en/5.0/topics/forms/formsets/#can-delete).

It looks like you’d want to check to see if `single_form in formsets.deleted_forms`, but that’s just a guess based on what I’m reading. (I’ve never had to do this manually.)

---

<div class="post-metadata">

**Author:** ![tjipkevdh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tjipkevdh/32/18254_2.png) [@tjipkevdh](https://forum.djangoproject.com/u/tjipkevdh)\
**Post date:** [January 9, 2024, 1:32pm UTC](https://forum.djangoproject.com/t/check-for-deletion-in-save-related-admin-py/26766/3 "2024-01-09T13:32:12Z")

</div>

Interesting, thank you a lot for your answer 🙂 I am rather more interested in checking for “is\_deleted” instead of “can\_delete”, cause if so, calling single\_form.save() would fail.  
I just discovered though, that all those forms that let you delete something, have a field associated with them called “DELETE”, which i can get in the save\_related method to see if it’s set to true or not. If it’s set to true, that means the object was deleted, if not, that means i can continue doing what i was doing 😃  
Thanks a lot for the help though, it is really appreciated 🙂

---

<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 9, 2024, 1:40pm UTC](https://forum.djangoproject.com/t/check-for-deletion-in-save-related-admin-py/26766/4 "2024-01-09T13:40:06Z")

</div>

> [@tjipkevdh](#):
>
> I am rather more interested in checking for “is\_deleted” instead of “can\_delete”,

The information about “is\_deleted” _is_ documented in the section for “can\_delete”.

---

<div class="post-metadata">

**Author:** ![tjipkevdh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tjipkevdh/32/18254_2.png) [@tjipkevdh](https://forum.djangoproject.com/u/tjipkevdh)\
**Post date:** [January 9, 2024, 1:49pm UTC](https://forum.djangoproject.com/t/check-for-deletion-in-save-related-admin-py/26766/5 "2024-01-09T13:49:28Z")

</div>

Looks like it suggests using that same ‘DELETE’ field, then that’s what i’ll use 🙂
