Calling save() on InventoryItem without changes produces unnecessary changelog entry #4998

Closed
opened 2025-12-29 19:23:01 +01:00 by adam · 3 comments
Owner

Originally created by @bluecmd on GitHub (Jun 12, 2021).

NetBox version

v2.11.6

Python version

3.7

Steps to Reproduce

  1. Call save() on an InventoryItem via Netbox Script:
it = InventoryItem.objects.all()[0]
it.save()
  1. Check changelog of the object
  2. The changelog only shows "Post-Change Data" and an empty "Pre-Change Data"

If I call .snapshot() before the "Pre-Change Data" is populated, and the UI shows "No difference".

Expected Behavior

Observed Behavior

An moot changelog entry is created.

Originally created by @bluecmd on GitHub (Jun 12, 2021). ### NetBox version v2.11.6 ### Python version 3.7 ### Steps to Reproduce 1. Call `save()` on an InventoryItem via Netbox Script: ```python3 it = InventoryItem.objects.all()[0] it.save() ``` 2. Check changelog of the object 3. The changelog only shows "Post-Change Data" and an empty "Pre-Change Data" If I call `.snapshot()` before the "Pre-Change Data" is populated, and the UI shows "No difference". ### Expected Behavior - Either mentioning that `.snapshot()` is important for Netbox Scripts on the https://netbox.readthedocs.io/en/stable/additional-features/custom-scripts/ website, or make it automatic, would be appreciated - That `.save()` with no changes is silent/ignored. ### Observed Behavior An moot changelog entry is created.
adam closed this issue 2025-12-29 19:23:01 +01:00
Author
Owner

@bluecmd commented on GitHub (Jun 12, 2021):

For now I have worked around this in my scripts by doing this:

-                iitem.part_id = psu['name']
-                iitem.serial = psu['serialNum']
-                iitem.discovered = True
-                iitem.save()
+                if iitem.part_id != psu['name'] or iitem.serial != psu['serialNum'] or not iitem.discovered:
+                    iitem.snapshot()
+                    iitem.part_id = psu['name']
+                    iitem.serial = psu['serialNum']
+                    iitem.discovered = True
+                    iitem.save()

If this is the intended way going forward I am OK with updating my scripts accordingly but ask that e.g. the example Custom Script is updated with this information on how to best do changes.

@bluecmd commented on GitHub (Jun 12, 2021): For now I have worked around this in my scripts by doing this: ```diff - iitem.part_id = psu['name'] - iitem.serial = psu['serialNum'] - iitem.discovered = True - iitem.save() + if iitem.part_id != psu['name'] or iitem.serial != psu['serialNum'] or not iitem.discovered: + iitem.snapshot() + iitem.part_id = psu['name'] + iitem.serial = psu['serialNum'] + iitem.discovered = True + iitem.save() ``` If this is the intended way going forward I am OK with updating my scripts accordingly but ask that e.g. the example Custom Script is updated with this information on how to best do changes.
Author
Owner

@jeremystretch commented on GitHub (Jun 14, 2021):

This intended behavior. Even when no data is being modified, there may still be a desire to record that a save event took place.

@jeremystretch commented on GitHub (Jun 14, 2021): This intended behavior. Even when no data is being modified, there may still be a desire to record that a save event took place.
Author
Owner

@bluecmd commented on GitHub (Jun 14, 2021):

Ok thanks for confirming.

I would suggest adding a section to https://netbox.readthedocs.io/en/stable/additional-features/custom-scripts/ describing the semantics around save() and snapshot(). It was quite hard going down the rabbit hole of Django data models and how things are supposed to work together, so that might be useful for people.

@bluecmd commented on GitHub (Jun 14, 2021): Ok thanks for confirming. I would suggest adding a section to https://netbox.readthedocs.io/en/stable/additional-features/custom-scripts/ describing the semantics around save() and snapshot(). It was quite hard going down the rabbit hole of Django data models and how things are supposed to work together, so that might be useful for people.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4998