Skip to content

Commit 31fb696

Browse files
14947 fix for missing changelog if only update m2m (#14986)
* 14947 fix for missing changelog if only update m2m * 14947 review change * 14947 DRY save logic * 14947 DRY save logic * Refactor logic --------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent b408bea commit 31fb696

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

netbox/extras/signals.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,23 @@ def handle_changed_object(sender, instance, **kwargs):
6868
else:
6969
return
7070

71-
# Record an ObjectChange if applicable
72-
if m2m_changed:
73-
ObjectChange.objects.filter(
71+
# Create/update an ObejctChange record for this change
72+
objectchange = instance.to_objectchange(action)
73+
# If this is a many-to-many field change, check for a previous ObjectChange instance recorded
74+
# for this object by this request and update it
75+
if m2m_changed and (
76+
prev_change := ObjectChange.objects.filter(
7477
changed_object_type=ContentType.objects.get_for_model(instance),
7578
changed_object_id=instance.pk,
7679
request_id=request.id
77-
).update(
78-
postchange_data=instance.to_objectchange(action).postchange_data
79-
)
80-
else:
81-
objectchange = instance.to_objectchange(action)
82-
if objectchange and objectchange.has_changes:
83-
objectchange.user = request.user
84-
objectchange.request_id = request.id
85-
objectchange.save()
80+
).first()
81+
):
82+
prev_change.postchange_data = objectchange.postchange_data
83+
prev_change.save()
84+
elif objectchange and objectchange.has_changes:
85+
objectchange.user = request.user
86+
objectchange.request_id = request.id
87+
objectchange.save()
8688

8789
# If this is an M2M change, update the previously queued webhook (from post_save)
8890
queue = events_queue.get()

0 commit comments

Comments
 (0)