Skip to content

Commit

Permalink
14947 fix for missing changelog if only update m2m (#14986)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
arthanson and jeremystretch authored Feb 5, 2024
1 parent b408bea commit 31fb696
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions netbox/extras/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,23 @@ def handle_changed_object(sender, instance, **kwargs):
else:
return

# Record an ObjectChange if applicable
if m2m_changed:
ObjectChange.objects.filter(
# Create/update an ObejctChange record for this change
objectchange = instance.to_objectchange(action)
# If this is a many-to-many field change, check for a previous ObjectChange instance recorded
# for this object by this request and update it
if m2m_changed and (
prev_change := ObjectChange.objects.filter(
changed_object_type=ContentType.objects.get_for_model(instance),
changed_object_id=instance.pk,
request_id=request.id
).update(
postchange_data=instance.to_objectchange(action).postchange_data
)
else:
objectchange = instance.to_objectchange(action)
if objectchange and objectchange.has_changes:
objectchange.user = request.user
objectchange.request_id = request.id
objectchange.save()
).first()
):
prev_change.postchange_data = objectchange.postchange_data
prev_change.save()
elif objectchange and objectchange.has_changes:
objectchange.user = request.user
objectchange.request_id = request.id
objectchange.save()

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

0 comments on commit 31fb696

Please sign in to comment.