Cumulative (running) sum with django orm and postgresql

前端 未结 5 895
你的背包
你的背包 2021-01-05 14:24

Is it possible to calculate the cumulative (running) sum using django\'s orm? Consider the following model:

class AModel(models.Model):
    a_number = models         


        
5条回答
  •  旧时难觅i
    2021-01-05 14:43

    For posterity, I found this to be a good solution for me. I didn't need the result to be a QuerySet, so I could afford to do this, since I was just going to plot the data using D3.js:

    import numpy as np
    import datettime
    
    today = datetime.datetime.date()
    
    raw_data = MyModel.objects.filter('date'=today).values_list('a_number', flat=True)
    
    cumsum = np.cumsum(raw_data)
    

提交回复
热议问题