diff --git a/apps/logs/models.py b/apps/logs/models.py index 17e8c710..be9a468b 100644 --- a/apps/logs/models.py +++ b/apps/logs/models.py @@ -1,6 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from django.contrib.contenttypes.models import ContentType from django.utils.translation import gettext_lazy as _ from django.conf import settings from django.core.exceptions import ValidationError @@ -20,8 +21,9 @@ class Changelog(models.Model): verbose_name=_('user'), ) - model = models.CharField( - max_length=255, + model = models.ForeignKey( + ContentType, + on_delete=models.PROTECT, null=False, blank=False, verbose_name=_('model'), diff --git a/apps/logs/signals.py b/apps/logs/signals.py index 646739fa..2ac25afc 100644 --- a/apps/logs/signals.py +++ b/apps/logs/signals.py @@ -3,6 +3,7 @@ import inspect +from django.contrib.contenttypes.models import ContentType from django.core import serializers from django.db.models.signals import pre_save, pre_delete from django.dispatch import receiver @@ -44,7 +45,7 @@ def save_object(sender, instance, **kwargs): previous_json = None instance_json = serializers.serialize('json', [instance, ],)[1:-1] Changelog.objects.create(user=user, - model=model_name, + model=ContentType.objects.get_for_model(instance), instance_pk=instance.pk, previous=previous_json, data=instance_json,