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
| Attribute | Type | Description |
|---|---|---|
| use_in_migrations | boolean = True | A 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
| Name | Type | Description |
|---|---|---|
| user_id | `int | str` |
| queryset | `QuerySet | Iterable` |
| action_flag | int | An integer constant representing the type of action performed, such as addition, change, or deletion. |
| change_message | `str | list` |
| single_object | bool = False | A flag that, when set to True, ensures the method returns a single LogEntry instance instead of a list when processing exactly one object. |
Returns
| Type | Description |
|---|---|
| `LogEntry | list[LogEntry]` |