Content type stored in logs instead of model name

This commit is contained in:
Yohann D'ANELLO 2020-02-26 23:34:27 +01:00
parent fd529a53c8
commit 1c0bf21cbe
2 changed files with 6 additions and 3 deletions

View File

@ -1,6 +1,7 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later # 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.utils.translation import gettext_lazy as _
from django.conf import settings from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -20,8 +21,9 @@ class Changelog(models.Model):
verbose_name=_('user'), verbose_name=_('user'),
) )
model = models.CharField( model = models.ForeignKey(
max_length=255, ContentType,
on_delete=models.PROTECT,
null=False, null=False,
blank=False, blank=False,
verbose_name=_('model'), verbose_name=_('model'),

View File

@ -3,6 +3,7 @@
import inspect import inspect
from django.contrib.contenttypes.models import ContentType
from django.core import serializers from django.core import serializers
from django.db.models.signals import pre_save, pre_delete from django.db.models.signals import pre_save, pre_delete
from django.dispatch import receiver from django.dispatch import receiver
@ -44,7 +45,7 @@ def save_object(sender, instance, **kwargs):
previous_json = None previous_json = None
instance_json = serializers.serialize('json', [instance, ],)[1:-1] instance_json = serializers.serialize('json', [instance, ],)[1:-1]
Changelog.objects.create(user=user, Changelog.objects.create(user=user,
model=model_name, model=ContentType.objects.get_for_model(instance),
instance_pk=instance.pk, instance_pk=instance.pk,
previous=previous_json, previous=previous_json,
data=instance_json, data=instance_json,