Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14947 fix for missing changelog if only update m2m #14986

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions netbox/extras/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,21 @@ def handle_changed_object(sender, instance, **kwargs):

# Record an ObjectChange if applicable
if m2m_changed:
ObjectChange.objects.filter(
qs = 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
)
if not qs:
objectchange = instance.to_objectchange(action)
if objectchange and objectchange.has_changes:
objectchange.user = request.user
objectchange.request_id = request.id
objectchange.save()
else:
qs.update(
postchange_data=instance.to_objectchange(action).postchange_data
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can clean this up a bit using the update_or_create() queryset method to automatically create a new change record where needed. We can also pull objectchange = instance.to_objectchange(action) up to the top, as it appears to be needed under every condition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the objectchange = line.

For update_or_create I don't think that would be any cleaner as on create you are doing objectchange.save and not specifying all the fields. For update_or_create you would need to specify all the fields for defaults and create_defaults which is sort of duplicating instance.to_objectchange.

else:
objectchange = instance.to_objectchange(action)
if objectchange and objectchange.has_changes:
Expand Down
Loading