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

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

Closed
bluecmd opened this issue Jun 12, 2021 · 3 comments

Comments

@bluecmd
Copy link

bluecmd commented 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.

@bluecmd bluecmd added the type: bug A confirmed report of unexpected behavior in the application label Jun 12, 2021
@bluecmd
Copy link
Author

bluecmd commented 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.

@jeremystretch jeremystretch removed the type: bug A confirmed report of unexpected behavior in the application label Jun 14, 2021
@jeremystretch
Copy link
Member

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

@bluecmd
Copy link
Author

bluecmd commented 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 bluecmd closed this as completed Jun 14, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants