nautobot.apps.change_logging - Nautobot Documentation

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.

:param user: User object :param request: WSGIRequest object to retrieve user from django rest framework after authentication is performed :param context: Context of the transaction, must match a choice in nautobot.extras.choices.ObjectChangeEventContextChoices :param context_detail: Optional extra details about the transaction (ex: the plugin name that initiated the change) :param change_id: Optional uuid object to uniquely identify the transaction. One will be generated if not supplied

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(). :param pre_object_data: Optional dictionary of serialized object data to be used in the object snapshot :param pre_object_data_v2: 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.

:param change_context: ChangeContext instance

## `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.

Example usage:

> > > 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()

:param user: User object :param context_detail: Optional extra details about the transaction (ex: the plugin name that initiated the change) :param change_id: Optional uuid object to uniquely identify the transaction. One will be generated if not supplied :param context: 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`. :param request: Optional web request instance, one will be generated if not supplied