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

60 Views Asked by At

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?

2

There are 2 best solutions below

0
dmitryro On BEST ANSWER

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.

0
D. Vargas On

You would need to know the value in both instances. For that you have to store total_spent every time it updates.