说我有一家水果店。我卖苹果,香蕉和其他水果。
有时我会在一小时内卖掉几件物品,有时甚至没有。
我想知道我卖的苹果最多的日期和我当天销售的数量......
(我正在使用带有django 1.5的sqlite3数据库)
我的模型看起来像:
class fruitsales(models.Model):
fruit = models.CharField(max_length=128, unique=False)
num_sold = models.IntegerField(default=0)
date = models.DateField()
def __unicode__(self):
return self.fruit
详尽的数据集可能如下所示:
fruit = "apple", num_sold = 3, date = 01/01/2015
fruit = "apple", num_sold = 5, date = 01/01/2015
fruit = "banana", num_sold = 24, date = 01/01/2015
fruit = "apple", num_sold = 6, date = 02/01/2015
我目前在views.py中做了类似的事情:
fruit = fruitData.objects.filter(fruitName=fruit***This is from the url and would in this
instance be:"apple"***).values('fruit').annotate(total_Sold=Sum('num_sold'))
返回;苹果,14,02 / 01/2015。而我希望; apple,8,01 / 01/2015
谢谢:D
答案 0 :(得分:0)
我认为这可以帮到你。 此代码未经过测试
试试这个:
fruitData.objects.filter(...).values('date').distinct().annotate(total_sold=Sum('num_sold')).values('fruit', 'total_sold')
答案 1 :(得分:0)
fruit = fruitData.objects.filter(fruitName='apple') \
.values('fruit', 'date') \
.annotate(total_sold=Sum('num_sold'))