Skip to main content

LogEntryManager

This class provides a custom manager for handling log entries, specifically facilitating the bulk creation of audit logs for model actions. It allows for logging changes across a queryset or for a single object by capturing user information, content types, and action flags. The manager automatically handles the serialization of change messages and ensures efficient database insertion using bulk operations.

Attributes

AttributeTypeDescription
use_in_migrationsboolean = TrueA boolean flag indicating that this manager should be available during data migrations.

Methods


log_actions()

@classmethod
def log_actions(
user_id: int | str,
queryset: QuerySet | Iterable,
action_flag: int,
change_message: str | list,
single_object: bool = False
) - > LogEntry | list[[LogEntry](logentry.md?sid=django_contrib_admin_models_logentry)]

Creates and persists audit log entries for a collection of model instances or a single object. This method handles the conversion of change messages to JSON and performs either a single save or a bulk creation depending on the size of the input queryset.

Parameters

NameTypeDescription
user_id`intstr`
queryset`QuerySetIterable`
action_flagintAn integer constant representing the type of action performed, such as addition, change, or deletion.
change_message`strlist`
single_objectbool = FalseA flag that, when set to True, ensures the method returns a single LogEntry instance instead of a list when processing exactly one object.

Returns

TypeDescription
`LogEntrylist[LogEntry]`