djangodjango-reversion

How to efficiently retrieve value of an attribute at a given time in Django Reversions?


I am currently using django-reversion to track a model Account which has a field total_spent.

Overtime this field gets updated whenever an accounts spends money. My goal is to retrieve the value of total_spent between X and Y, which are both datetime instances.

How can I do so efficiently?


Solution

  • You'd have something like this: given you have a Django model and two time fields, get their elapsed time difference:

    class SomeModel(models.Model):
        start_time = models.DateTimeField(blank=True, null=True)
        end_time = models.DateTimeField(blank=True, null=True)
    
        @property
        def total_spent(self):
            return self.end_time - self.start_time
    

    and then use total_spent as yet another property of that model.