Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django simple history is not working when changed from the application

I am using django-simple-history for recording the change history in database models. When I change a particular value from the model history is visible in the admin panel. But when value edited or changed from the application, the history is not visible in the admin panel. Is there something which I am missing.

from django.db import models
from simple_history.models import HistoricalRecords

class AcademicYear(models.Model):
    academic_year_start = models.PositiveIntegerField(blank=False, null=False, max_length=4)
    academic_year_end = models.PositiveIntegerField(blank=False, null=False, max_length=4)
    history = HistoricalRecords()


    def __unicode__(self):
        return "%s-%s" % (self.academic_year_start, self.academic_year_end)

class Activity(models.Model):
    activity_name = models.CharField(max_length=100, blank=False)
    history = HistoricalRecords()

    def __unicode__(self):
        return "%s" % self.activity_name

The update


I investigated my In the views instead of .save() I am using .update() for updating my model values.

activity_obj_list = Activity.objects.filter(activity_name=name)
activity_obj_list.update(activity_name=new_activity_name)

the update function does not create the history log. Is this some kind of bug ?

like image 557
vdkotian Avatar asked Mar 16 '23 03:03

vdkotian


1 Answers

This isn't a bug, it's the expected behaviour because the .update() doesn't send the pre/post save signals. If you want the history to be triggered iterate through your queryset performing a save() rather than using the .update()

for obj in queryset:
    obj.field1 = some_value
    obj.save()
like image 103
Nick Avatar answered Apr 08 '23 08:04

Nick