Home > Mobile >  I want to create a report of all update history of my lead model in django
I want to create a report of all update history of my lead model in django

Time:12-01

I have a Training_Lead model, in my team I have 5-6 members who can edit this lead from there id's. I want to create a report of update history that who is update lead and when, fro that i have create a two column names last_modification and last_modification_time which are automatically updated when someone updates the lead.

class Training_Lead(models.Model):
handel_by = models.ForeignKey(UserInstance, on_delete=models.PROTECT)
learning_partner = models.ForeignKey(
    Learning_Partner, on_delete=models.PROTECT, blank=False, null=False)
assign_to_trainer = models.ForeignKey(
    Trainer, on_delete=models.PROTECT, null=True, blank=True)
course_name = models.CharField(max_length=2000)
lead_type = models.CharField(max_length=2000)
time_zone = models.CharField(choices=(('IST', 'IST'), ('GMT', 'GMT'), ('BST', 'BST'), (
    'CET', 'CET'), ('SAST', 'SAST'), ('EST', 'EST'), ('PST', 'PST'), ('MST', 'MST'), ('UTC', 'UTC')), max_length=40, blank=False, null=False)
getting_lead_date = models.DateTimeField(null=True, blank=True)
start_date = models.DateTimeField(null=True, blank=True)
end_date = models.DateTimeField(null=True, blank=True)
lead_status = models.CharField(choices=(('Initial', 'Initial'), ('In Progress', 'In Progress'), ('Follow Up', 'Follow Up'), (
    'Cancelled', 'Cancelled'), ('Confirmed', 'Confirmed'), ('PO Received', 'PO Received')), max_length=40, blank=False, null=False)
lead_description = models.CharField(max_length=9000, blank=True, null=True)
last_modification = models.CharField(null=False, blank=False, max_length=500)
last_modification_time = models.DateTimeField(auto_now_add='True')



def __str__(self):
    return str(self.assign_to_trainer)

class Meta:
    ordering = ['start_date']

CodePudding user response:

Create a new model to hold the history data, this table has all the history of the records. Training_LeadHist may have all the columns of Training_Lead the details of who made the change, when etc. Better not to use foreign keys instead use ids. If a record is edited 2 times the History table has 2 entries for the corrosponding training id.

Training_LeadHist(id, training_lead_id, learning_partner_id, course_name , created_dt, updated_dt, ...)

You can use Django signals to save the data o the new History table. Create a file (say) signals.py in the app.

Pre_save (django.db.models.signals.pre_save) is called before the model save() method is called, So the following function will invoked just before saving/update the latest data to Training_Lead. refer There you fetch the existing object and save the state to you new history table.

from django.db.models.signals import pre_save

@receiver(pre_save, sender=Training_Lead)
def save_to_lead_hist(sender, instance, **kwargs):
    previous_obj = Training_Lead.objects.get(pk=instance.id)
    hist = {'training_lead_id':previous_obj.id,  'handel_by_id: previous_obj.handel_by.id,  'course_name': previous_obj.course_name, .... }
    Training_Lead.objects.create(**hist)

And add/modify the ready function of app.py ( where the signals.py resided)

Appname/app.py
class AppNameConfig(AppConfig):
    
    def ready(self):
        import app.signals # add this line
    
Appname/__init__.py
    default_app_config = 'Appname.apps.AppNameConfig'

or check the django package

  • Related