diff --git a/apps/audits/models.py b/apps/audits/models.py index aab3bbde9..bd7988dbe 100644 --- a/apps/audits/models.py +++ b/apps/audits/models.py @@ -5,7 +5,7 @@ from django.db.models import Q from django.utils.translation import ugettext_lazy as _ from django.utils import timezone -from orgs.mixins.models import OrgModelMixin +from orgs.mixins.models import OrgModelMixin, Organization from orgs.utils import current_org __all__ = [ @@ -63,6 +63,11 @@ class OperateLog(OrgModelMixin): def __str__(self): return "<{}> {} <{}>".format(self.user, self.action, self.resource) + def save(self, *args, **kwargs): + if current_org.is_root() and not self.org_id: + self.org_id = Organization.ROOT_ID + return super(OperateLog, self).save(*args, **kwargs) + class PasswordChangeLog(models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True) diff --git a/apps/audits/signals_handler.py b/apps/audits/signals_handler.py index 05de237d3..79b936c83 100644 --- a/apps/audits/signals_handler.py +++ b/apps/audits/signals_handler.py @@ -199,7 +199,7 @@ def on_m2m_changed(sender, action, instance, reverse, model, pk_set, **kwargs): resource = resource_tmpl.format(**{ instance_name: instance_value, model_name: str(obj) - }) + })[:128] # `resource` 字段只有 128 个字符长 😔 to_create.append(OperateLog( user=user, action=action, resource_type=resource_type,