pythondjangosqlitehistorical-db

How to store historical data using Django


I am new to Django and am trying to figure out how to store historical data and be able to reference any data point from a date in the past. Say I want to store data on a kids height change as he grows and be able to compare his height at day 400 to his height at day 6xx. How would I set this up in Django? Would there need to be a foreign key relationship between height and the kid?


Solution

  • You can use simple history app. According to the docs you need to add a HistoricalRecords field to your model:

    from django.db import models
    from simple_history.models import HistoricalRecords
    
    class Kid(models.Model):
         id = models.IntegerField(primary_key=True)
         height = models.IntegerField()
         history = HistoricalRecords()
    

    And then all model changes will be stored in the separate table. You can use this field in the code or use admin views to check historical records and retrieve.