django / python计算在特定月份插入的行数

时间:2017-03-30 08:11:08

标签: python django datetime count

Purchase_History模型

class Purchase_History(models.Model):
   barcode = models.BigIntegerField()
   email = models.CharField(max_length=100)
   bill_no = models.IntegerField()
   longitude = models.CharField(max_length=500)
   latitude = models.CharField(max_length=500)
   timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)

我想计算特定月份的行数 我试过像

这样的东西
m=Purchase_History.objects.filter(timestamp="month").count()

m=Purchase_History.objects.filter(timestamp=["2017-01-01", "2017-01-31"]).count()

使用时间戳字段的好习惯,请帮助我,如果你有任何解决方案。

提前致谢

1 个答案:

答案 0 :(得分:2)

您可以使用__range过滤器:

Purchase_History.objects.filter(timestamp__range=["2017-01-01", "2017-01-31"]).count()
相关问题