Log pre-existing object state when creating an ObjectChange instance #4617

Closed
opened 2025-12-29 18:38:19 +01:00 by adam · 2 comments
Owner

Originally created by @jeremystretch on GitHub (Mar 3, 2021).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.7.9
  • NetBox version: 2.10.5

Proposed Functionality

When creating a new ObjectChange instance to log a change to a NetBox object, capture its pre-change state as well as its current state. This may take a bit of cleverness to implement, but it should be possible by using a custom signal to register a pre-change copy of data on an instance immediately prior to modifying it.

Use Case

Storing both the pre- and post-change copies of an object allows us to present a more accurate depiction of that changes attributable to a specific action, and not worry about indirect changes not being accounted for. Related issues:

  • #2511: Provide a diff showing the changes between two logged changes (implemented)
  • #3451: WebHook should provide old data together with new data
  • #5583: Unnecessary changelog records for tag add/change/remove

Database Changes

Add a field to the ObjectChange model for storing pre-change data.

External Dependencies

None expected

Originally created by @jeremystretch on GitHub (Mar 3, 2021). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.7.9 * NetBox version: 2.10.5 ### Proposed Functionality When creating a new ObjectChange instance to log a change to a NetBox object, capture its pre-change state as well as its current state. This may take a bit of cleverness to implement, but it should be possible by using a custom signal to register a pre-change copy of data on an instance immediately prior to modifying it. ### Use Case Storing both the pre- and post-change copies of an object allows us to present a more accurate depiction of that changes attributable to a specific action, and not worry about indirect changes not being accounted for. Related issues: * #2511: Provide a diff showing the changes between two logged changes (implemented) * #3451: WebHook should provide old data together with new data * #5583: Unnecessary changelog records for tag add/change/remove ### Database Changes Add a field to the ObjectChange model for storing pre-change data. ### External Dependencies None expected
adam added the status: acceptedtype: feature labels 2025-12-29 18:38:19 +01:00
adam closed this issue 2025-12-29 18:38:19 +01:00
Author
Owner

@jeremystretch commented on GitHub (Mar 3, 2021):

This is what I've come up with so far: The ObjectChange model has been extended to store both pre- and post-change data. When a request is processed, the responsible view calls snapshot() on the object being modified or deleted, which saves a serialized representation of the object on the object itself generated by the serialize_object() utility. After the change has been made, serialize_object() is called on the object once more to catch the post-change data. Both of these serialized representations are then stored in the new ObjectChange instance.

Screenshot_2021-03-03 dcim site Site X updated by admin - NetBox

I'd like to add some GitHub-style diff highlighting to the UI, but the important bits are all there. I also still need to write changelog tests for bulk edit and delete views.

@jeremystretch commented on GitHub (Mar 3, 2021): This is what I've come up with so far: The ObjectChange model has been extended to store both pre- and post-change data. When a request is processed, the responsible view calls `snapshot()` on the object being modified or deleted, which saves a serialized representation of the object on the object itself generated by the `serialize_object()` utility. After the change has been made, `serialize_object()` is called on the object once more to catch the post-change data. Both of these serialized representations are then stored in the new ObjectChange instance. ![Screenshot_2021-03-03 dcim site Site X updated by admin - NetBox](https://user-images.githubusercontent.com/13487278/109878962-f846b000-7c42-11eb-98e7-e9c266d7217f.png) I'd like to add some GitHub-style diff highlighting to the UI, but the important bits are all there. I also still need to write changelog tests for bulk edit and delete views.
Author
Owner

@jeremystretch commented on GitHub (Mar 4, 2021):

Now with diff highlighting!

Screenshot_2021-03-04 ipam prefix 192 0 2 0 24 updated by admin - NetBox

I expect we'll put a bit more polish on it in conjunction with the ongoing UI overhaul (#5893) prior to release.

@jeremystretch commented on GitHub (Mar 4, 2021): Now with diff highlighting! ![Screenshot_2021-03-04 ipam prefix 192 0 2 0 24 updated by admin - NetBox](https://user-images.githubusercontent.com/13487278/110009068-80cc5b80-7cea-11eb-908b-31235139fb5a.png) I expect we'll put a bit more polish on it in conjunction with the ongoing UI overhaul (#5893) prior to release.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4617