HorillaCoreModel includes AuditlogHistoryField for field-level history. Deleted records can land in the recycle bin for restore before permanent removal.

This post is Part 27 of 28 in the Horilla CRM Technical Blog series.

What is an audit log and recycle bin?

audit log and recycle bin enable you to:

  • Per-field change history on models
  • History tab on detail pages
  • Soft delete and recycle bin UI
  • Field permissions apply to history display

Audit trail integration

Horilla CRM uses django-auditlog for model history. The project enables AUDITLOG_INCLUDE_ALL_MODELS, so audit tracking is broadly configured rather than requiring every feature developer to remember an individual model registration. HorillaCoreModel exposes history through AuditlogHistoryField, giving models that inherit the core base a standard relation for recording creation, updates, and deletions. This provides a consistent history surface across company-scoped CRM records without each app inventing a custom audit table.

Audit data is evidence, not an authorization bypass. A history presentation must still honor the application’s permission and field-visibility rules. In particular, do not expose hidden field values merely because auditlog captured a change. When building a detail tab, use the normal server-side permission context and redact or omit values the viewer cannot read. Also distinguish an audit event’s actor from a record’s current owner: the former indicates who made a change; the latter indicates responsibility for the current record.

from horilla.contrib.core.models import HorillaCoreModel
from horilla.db import models
class CustomerNote(HorillaCoreModel):
    body = models.TextField()
    # history is inherited as AuditlogHistoryField()

AUDITLOG_INCLUDE_ALL_MODELS reduces configuration omission risk, but it does not make arbitrary sensitive data safe to retain forever. Review secret-bearing fields and data retention obligations before adding them to any model, and ensure application logs do not serialize audit records with unredacted values.

Recycle bin model and policy

Recovery behavior lives in horilla.contrib.core, specifically RecycleBin and RecycleBinPolicy. RecycleBin records enough information about a deleted object to identify it and make restoration possible. RecycleBinPolicy supplies the retention and policy controls governing how long recoverable entries remain. These are core services because a deleted lead, account, activity, or another registered domain model needs a common recovery path rather than a separate recycle table in each app.

The generic delete mixins implement soft-delete behavior and the recycle-bin integration. Use those mixins for normal UI deletion flows rather than calling instance.delete() from an ad hoc view. The generic path preserves the expected permission checks, deactivation/soft-delete semantics, and recovery metadata. A bulk-delete action deserves the same care: evaluate every selected row through the generic authorization and deletion path instead of treating a visible list selection as permission to delete all rows.

from horilla.contrib.core.models.recyclebin import RecycleBin, RecycleBinPolicy
policy = RecycleBinPolicy.objects.first()
recoverable = RecycleBin.objects.filter(is_active=True)

The field names in a concrete query should follow the installed models; the example shows the core ownership of recovery data, not a replacement policy implementation.

Restore operation and collisions

restore_recycle_bin_records in horilla.contrib.core.utils is the restoration entry point. It accepts the request and recycle-bin records, allowing the operation to use request context for authorization, feedback, and active-company scoping. Restoring should be transactional wherever the record and its required related state can be restored atomically. A restore can fail legitimately when a unique value was reused, a referenced record was permanently removed, or the requester no longer has access to the target company. Report those failures precisely; silently skipping records produces an unreliable recovery interface.

from horilla.contrib.core.utils import restore_recycle_bin_records
restore_recycle_bin_records(request, selected_recycle_entries)

Do not recreate an object manually from serialized recycle-bin content in a view. That bypasses the core utility’s type handling and can miss model invariants. Build the UI around selecting eligible RecycleBin entries and delegate restoration to the shared function.

Expiration and operational control

clear_expired_recyclebin in horilla.contrib.core.scheduler removes entries whose policy retention has elapsed. Schedule it from the approved background scheduling mechanism and test it against policy boundaries. It should be treated as destructive maintenance: once expired recycle data is cleared, restore is no longer available. Use a retention period compatible with business, legal, backup, and customer-support requirements, and ensure database backups have their own independently tested restore procedure.

Audit history and the recycle bin answer different questions. Auditlog explains what changed and who performed an action; the recycle bin offers a bounded chance to restore deleted business data. Retaining an audit entry does not guarantee that the deleted object can be restored, and restoring an object does not erase the deletion event. Keeping those semantics explicit makes support investigations, compliance reviews, and accidental-delete recovery predictable.

Benefits of Audit Log in Horilla CRM

  • Compliance and debugging
  • Undo accidental deletes
  • Transparent data changes per user

Audit and recycle bin ship with HorillaCoreModel — enable history tabs and train users on restore workflows for safety.

Continue the series

Previous: Part 26 — How Horilla CRM Handles Background Tasks Using Celery, Beat, and APScheduler

Next: Part 28 — How to Deploy Horilla CRM to Production with Docker, PostgreSQL, Redis & ASGI

More posts are on the Horilla Blogs; share feedback on GitHub.

Share this article