from django.conf import settings from django.db import models class OperationAuditLog(models.Model): user = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="operation_audit_logs", ) module = models.CharField(max_length=80) action = models.CharField(max_length=80) target_type = models.CharField(max_length=80) target_id = models.CharField(max_length=120, blank=True) request_snapshot = models.JSONField(default=dict, blank=True) result = models.CharField(max_length=20) ip_address = models.GenericIPAddressField(null=True, blank=True) user_agent = models.TextField(blank=True) created_at = models.DateTimeField(auto_now_add=True) class Meta: ordering = ["-created_at"] indexes = [ models.Index(fields=["module", "action"]), models.Index(fields=["target_type", "target_id"]), models.Index(fields=["created_at"]), ] def __str__(self) -> str: return f"{self.module}.{self.action}:{self.result}"