文章詳細地址:https://tengxiaotao.top/blog/7
模型(models.py)
class VisitNumber(models.Model):
count=models.IntegerField(verbose_name='網(wǎng)站訪問總次數(shù)',default=0) #網(wǎng)站訪問總次數(shù)
class Meta:
verbose_name = '網(wǎng)站訪問總次數(shù)'
verbose_name_plural = verbose_name
def __str__(self):
return str(self.count)
視圖(views.py)
對某一列中的值進行求和
views.py
from django.db.models import Sum # 引入
visitNumber = VisitNumber.objects.aggregate(nums=Sum('count'))
print(visitNumber['nums'])
對符合條件的數(shù)據(jù)進行求和
total=ImportData.objects.filter('列名'='值').aggregate(nums=Sum('count'))
print total['nums']
根據(jù)條件進行分組求和
reg=ImportDta.object.values('列名').annotate(number=Sum("count"))
#[{'product': u'E', 'number': 3637}, {'product': u'S', 'number': 19409}, {'product': u'C', 'number': 4829}, {'product': u'N', 'number': 846}]
參考鏈接:https://docs.djangoproject.com/en/2.1/topics/db/aggregation/