[PR #19308] [MERGED] Fixes: #18717 - On delete signal handling, manually save the related object in a ManyToOneRel to trigger a change record #15601

Closed
opened 2025-12-30 00:22:57 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/19308
Author: @bctiemann
Created: 4/24/2025
Status: Merged
Merged: 4/24/2025
Merged by: @jnovinger

Base: mainHead: 18717-trigger-object-change-on-related-many-to-one-field


📝 Commits (2)

  • ccfa4fe On delete signal handling, manually save the related object in a ManyToOneRel to trigger a change record
  • c705a49 Only set remote field to None if null=True on the relation

📊 Changes

1 file changed (+9 additions, -3 deletions)

View changed files

📝 netbox/core/signals.py (+9 -3)

📄 Description

Fixes: #18717

The handle_deleted_object signal handler ensures that change records are saved for the object being deleted, but also manually triggers a m2m_changed signal to ensure that related objects through many-to-many relationships are also change-logged through their own signal handlers. However, we do not handle the case where the related object is linked to the deleted object through a many-to-one relationship.

This change handles ManyToOneRel fields in the same area of code, by resolving the related object in the same way as with M2M fields, but instead setting the related object's field value to None and saving, thus ensuring a change record is created for the related object.

Note the check on L164 which ensures the related field is null=True. This is primarily to ensure that unit tests pass, as without it one of the tests fails due to trying to set a non-nullable field to None. However I think in practice this will not happen as in the case of any FK relationship where null=False, trying to delete an object which is pointed to by a related object via a many-to-one relationship will be caught by object-level validation before the deleted object handler is ever called.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/19308 **Author:** [@bctiemann](https://github.com/bctiemann) **Created:** 4/24/2025 **Status:** ✅ Merged **Merged:** 4/24/2025 **Merged by:** [@jnovinger](https://github.com/jnovinger) **Base:** `main` ← **Head:** `18717-trigger-object-change-on-related-many-to-one-field` --- ### 📝 Commits (2) - [`ccfa4fe`](https://github.com/netbox-community/netbox/commit/ccfa4fe47dc9dbf6d212d35ca2ada0db1648b510) On delete signal handling, manually save the related object in a ManyToOneRel to trigger a change record - [`c705a49`](https://github.com/netbox-community/netbox/commit/c705a49830405c71fe33a6d9e7c465d2b9049a2d) Only set remote field to None if null=True on the relation ### 📊 Changes **1 file changed** (+9 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `netbox/core/signals.py` (+9 -3) </details> ### 📄 Description ### Fixes: #18717 The `handle_deleted_object` signal handler ensures that change records are saved for the object being deleted, but also manually triggers a `m2m_changed` signal to ensure that related objects through many-to-many relationships are also change-logged through their own signal handlers. However, we do not handle the case where the related object is linked to the deleted object through a many-to-one relationship. This change handles `ManyToOneRel` fields in the same area of code, by resolving the related object in the same way as with M2M fields, but instead setting the related object's field value to `None` and saving, thus ensuring a change record is created for the related object. **Note** the check on L164 which ensures the related field is `null=True`. This is primarily to ensure that unit tests pass, as without it one of the tests fails due to trying to set a non-nullable field to `None`. However I think in practice this will not happen as in the case of any FK relationship where `null=False`, trying to delete an object which is pointed to by a related object via a many-to-one relationship will be caught by object-level validation before the deleted object handler is ever called. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:22:57 +01:00
adam closed this issue 2025-12-30 00:22:58 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15601