Skip to content

nautobot.apps.change_logging

Classes and utilities related to Nautobot change logging.

nautobot.apps.change_logging.ChangeContext

ChangeContext is used to describe a single transaction that may be related to one or more object changes. A unique id can be provided, otherwise one will be generated to relate any changes to this transaction. Convenience classes are provided for each context.

Parameters:

Name Type Description Default
user User

User object

None
request WSGIRequest

object to retrieve user from django rest framework after authentication is performed

None
context ObjectChangeEventContextChoices

Context of the transaction

None
context_detail Optional[str]

extra details about the transaction (ex: plugin name that initiated the change)

''
change_id Optional[UUID]

Object to uniquely identify the transaction. One will be generated if not supplied

None

The next two parameters are used as caching fields when updating an object with no previous ObjectChange instances. They are used to populate the pre_change field of an ObjectChange snapshot in get_snapshot().

pre_object_data (dict): Optional dictionary of serialized object data to be used in the object snapshot
pre_object_data_v2 (dict): Optional dictionary of serialized object data to be used in the object snapshot

as_dict(instance=None)

Return ChangeContext attributes in dictionary format

get_user(instance=None)

Return self.user if set, otherwise return self.request.user

nautobot.apps.change_logging.JobChangeContext

Bases: ChangeContext

ChangeContext for changes made by jobs

nautobot.apps.change_logging.JobHookChangeContext

Bases: ChangeContext

ChangeContext for changes made by job hooks

nautobot.apps.change_logging.ORMChangeContext

Bases: ChangeContext

ChangeContext for changes made with web_request_context context manager

nautobot.apps.change_logging.WebChangeContext

Bases: ChangeContext

ChangeContext for changes made through the web interface

nautobot.apps.change_logging.change_logging(change_context)

Enable change logging by connecting the appropriate signals to their receivers before code is run, and disconnecting them afterward.

nautobot.apps.change_logging.web_request_context(user, context_detail='', change_id=None, context=ObjectChangeEventContextChoices.CONTEXT_ORM, request=None)

Emulate the context of an HTTP request, which provides functions like change logging and webhook processing in response to data changes. This context manager is for use with low level utility tooling, such as the 'nautobot-server nbshell' management command.

By default, when working with the Django ORM, neither change logging nor webhook processing occur unless manually invoked and this context manager handles those functions. A valid User object must be provided.

Examples:

>>> from nautobot.extras.context_managers import web_request_context
>>> user = User.objects.get(username="admin")
>>> with web_request_context(user, context_detail="manual-fix"):
...     lt = Location.objects.get(name="Root")
...     lax = Location(name="LAX", location_type=lt)
...     lax.validated_save()

Parameters:

Name Type Description Default
user User

User object

required
context_detail str

Optional extra details about the transaction (ex: plugin name that initiated the change)

''
change_id Optional[UUID]

Object to uniquely identify the transaction. One will be generated if not supplied

None
context str

Optional string value of the generated change log entries' "change_context" field. Defaults to ObjectChangeEventContextChoices.CONTEXT_ORM. Valid choices are in nautobot.extras.choices.ObjectChangeEventContextChoices.

CONTEXT_ORM
request Request

Optional web request instance, one will be generated if not supplied

None