Document how to generate Pre-Change Data for existing objects on the Custom Scripts page #6621

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

Originally created by @hagbarddenstore on GitHub (Jul 2, 2022).

Originally assigned to: @DanSheps on GitHub.

Change Type

Addition

Area

Functionality/features

Proposed Changes

The documentation page for Custom Scripts covers how to create new objects, but doesn't cover the caveats when updating an existing object.

Suggestion

Writing Custom Scripts

....the already existing text...

To generate the correct change log data when editing an existing object, a snapshot of the object must be taken before making any changes to the object.

if obj.pk and hasattr(obj, 'snapshot'):
    obj.snapshot()

obj.property = "New Value"

obj.save()
Originally created by @hagbarddenstore on GitHub (Jul 2, 2022). Originally assigned to: @DanSheps on GitHub. ### Change Type Addition ### Area Functionality/features ### Proposed Changes The documentation page for Custom Scripts covers how to create new objects, but doesn't cover the caveats when updating an existing object. ### Suggestion #### Writing Custom Scripts ....the already existing text... To generate the correct change log data when editing an existing object, a snapshot of the object must be taken before making any changes to the object. ```python if obj.pk and hasattr(obj, 'snapshot'): obj.snapshot() obj.property = "New Value" obj.save() ```
adam added the status: acceptedtype: documentation labels 2025-12-29 19:43:13 +01:00
adam closed this issue 2025-12-29 19:43:13 +01:00
Author
Owner

@github-actions[bot] commented on GitHub (Sep 4, 2022):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Sep 4, 2022): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@candlerb commented on GitHub (Sep 18, 2022):

To generate the correct change log data when editing an existing object, a snapshot of the object must be taken before making any changes to the object.

And if you're going to do this properly, make sure you include a full_clean() as well:

...
obj.property = "New Value"
obj.full_clean()      # <<<<< NOTE!
obj.save()

Otherwise, you can set obj.property to an invalid value, and it will still be saved successfully.

@candlerb commented on GitHub (Sep 18, 2022): > To generate the correct change log data when editing an existing object, a snapshot of the object must be taken before making any changes to the object. And if you're going to do this properly, make sure you include a [`full_clean()`](https://stackoverflow.com/questions/4441539/why-doesnt-djangos-model-save-call-full-clean) as well: ``` ... obj.property = "New Value" obj.full_clean() # <<<<< NOTE! obj.save() ``` Otherwise, you can set `obj.property` to an invalid value, and it will still be saved successfully.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6621