Mohammad
Mohammad

Reputation: 1

Values between two dates

I have a database in Django contains amounts and values stored by dates, I want to calculate the values or the amounts between 2 dates how to do that ?

Date           the value
01/01/2018     500$
01/02/2018     700$
01/03/2018     800$

how to calculate the values from 01/01/2018 to 01/02/2018 for example ?

Upvotes: 0

Views: 270

Answers (1)

Lemayzeur
Lemayzeur

Reputation: 8525

First off, a queryset of value between the range data:

queryset = Model.objects.filter(
                    Q(date__gte = date_start) & 
                    Q(date__lte = date_end)
            )

And then with Sum, you can have the sum of value

queryset.aggreage(total=Sum('value'))

Full code:

>>> from django.db.moedls import Q, Sum

>>> Model.objects.filter(
                    Q(date__gte = date_start) & 
                    Q(date__lte = date_end)
            ).aggreage(total=Sum('value'))

>>> {'total': 'a_number_here'}

date_start, date_end should be date or datetime object

Upvotes: 1

Related Questions