本文整理匯總了Python中django.db.models.aggregates.Sum方法的典型用法代碼示例。如果您正苦於以下問題:Python aggregates.Sum方法的具體用法?Python aggregates.Sum怎麽用?Python aggregates.Sum使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.models.aggregates
的用法示例。
在下文中一共展示了aggregates.Sum方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: check_expression_support
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def check_expression_support(self, expression):
bad_fields = (fields.DateField, fields.DateTimeField, fields.TimeField)
bad_aggregates = (aggregates.Sum, aggregates.Avg, aggregates.Variance, aggregates.StdDev)
if isinstance(expression, bad_aggregates):
for expr in expression.get_source_expressions():
try:
output_field = expr.output_field
except FieldError:
# Not every subexpression has an output_field which is fine
# to ignore.
pass
else:
if isinstance(output_field, bad_fields):
raise NotImplementedError(
'You cannot use Sum, Avg, StdDev, and Variance '
'aggregations on date/time fields in sqlite3 '
'since date/time is saved as text.'
)
示例2: spend
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def spend(self, start, finish):
"""
@param start: the start date the the data is for.
@param finish: the finish date you want the data for.
"""
account_first_synced = DailyAccountMetrics.objects.filter(account=self).aggregate(Min('day'))
first_synced_date = None
if 'day__min' in account_first_synced:
first_synced_date = account_first_synced['day__min']
if not self.account_last_synced or self.account_last_synced < finish or not first_synced_date or first_synced_date > start:
raise AdwordsDataInconsistencyError('Google Adwords Account %s does not have correct amount of data to calculate the spend between "%s" and "%s"' % (
self,
start,
finish,
))
cost = self.metrics.filter(day__gte=start, day__lte=finish).aggregate(Sum('cost'))['cost__sum']
if cost is None:
return 0
else:
return cost
示例3: check_expression_support
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def check_expression_support(self, expression):
bad_fields = (fields.DateField, fields.DateTimeField, fields.TimeField)
bad_aggregates = (aggregates.Sum, aggregates.Avg, aggregates.Variance, aggregates.StdDev)
if isinstance(expression, bad_aggregates):
for expr in expression.get_source_expressions():
try:
output_field = expr.output_field
except FieldError:
# Not every subexpression has an output_field which is fine
# to ignore.
pass
else:
if isinstance(output_field, bad_fields):
raise utils.NotSupportedError(
'You cannot use Sum, Avg, StdDev, and Variance '
'aggregations on date/time fields in sqlite3 '
'since date/time is saved as text.'
)
示例4: check_expression_support
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def check_expression_support(self, expression):
bad_fields = (fields.DateField, fields.DateTimeField, fields.TimeField)
bad_aggregates = (aggregates.Sum, aggregates.Avg, aggregates.Variance, aggregates.StdDev)
if isinstance(expression, bad_aggregates):
for expr in expression.get_source_expressions():
try:
output_field = expr.output_field
if isinstance(output_field, bad_fields):
raise NotImplementedError(
'You cannot use Sum, Avg, StdDev, and Variance '
'aggregations on date/time fields in sqlite3 '
'since date/time is saved as text.'
)
except FieldError:
# Not every subexpression has an output_field which is fine
# to ignore.
pass
示例5: test_department_salary
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def test_department_salary(self):
qs = Employee.objects.annotate(department_sum=Window(
expression=Sum('salary'),
partition_by=F('department'),
order_by=[F('hire_date').asc()],
)).order_by('department', 'department_sum')
self.assertQuerysetEqual(qs, [
('Jones', 'Accounting', 45000, 45000),
('Jenson', 'Accounting', 45000, 90000),
('Williams', 'Accounting', 37000, 127000),
('Adams', 'Accounting', 50000, 177000),
('Wilkinson', 'IT', 60000, 60000),
('Moore', 'IT', 34000, 94000),
('Miller', 'Management', 100000, 100000),
('Johnson', 'Management', 80000, 180000),
('Smith', 'Marketing', 38000, 38000),
('Johnson', 'Marketing', 40000, 78000),
('Smith', 'Sales', 55000, 55000),
('Brown', 'Sales', 53000, 108000),
], lambda entry: (entry.name, entry.department, entry.salary, entry.department_sum))
示例6: test_multiple_ordering
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def test_multiple_ordering(self):
"""
Accumulate the salaries over the departments based on hire_date.
If two people were hired on the same date in the same department, the
ordering clause will render a different result for those people.
"""
qs = Employee.objects.annotate(sum=Window(
expression=Sum('salary'),
partition_by='department',
order_by=[F('hire_date').asc(), F('name').asc()],
)).order_by('department', 'sum')
self.assertQuerysetEqual(qs, [
('Jones', 45000, 'Accounting', datetime.date(2005, 11, 1), 45000),
('Jenson', 45000, 'Accounting', datetime.date(2008, 4, 1), 90000),
('Williams', 37000, 'Accounting', datetime.date(2009, 6, 1), 127000),
('Adams', 50000, 'Accounting', datetime.date(2013, 7, 1), 177000),
('Wilkinson', 60000, 'IT', datetime.date(2011, 3, 1), 60000),
('Moore', 34000, 'IT', datetime.date(2013, 8, 1), 94000),
('Miller', 100000, 'Management', datetime.date(2005, 6, 1), 100000),
('Johnson', 80000, 'Management', datetime.date(2005, 7, 1), 180000),
('Smith', 38000, 'Marketing', datetime.date(2009, 10, 1), 38000),
('Johnson', 40000, 'Marketing', datetime.date(2012, 3, 1), 78000),
('Smith', 55000, 'Sales', datetime.date(2007, 6, 1), 55000),
('Brown', 53000, 'Sales', datetime.date(2009, 9, 1), 108000),
], transform=lambda row: (row.name, row.salary, row.department, row.hire_date, row.sum))
示例7: test_range_unbound
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def test_range_unbound(self):
"""A query with RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING."""
qs = Employee.objects.annotate(sum=Window(
expression=Sum('salary'),
partition_by='department',
order_by=[F('hire_date').asc(), F('name').asc()],
frame=ValueRange(start=None, end=None),
)).order_by('department', 'hire_date', 'name')
self.assertIn('RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING', str(qs.query))
self.assertQuerysetEqual(qs, [
('Jones', 'Accounting', 45000, datetime.date(2005, 11, 1), 177000),
('Jenson', 'Accounting', 45000, datetime.date(2008, 4, 1), 177000),
('Williams', 'Accounting', 37000, datetime.date(2009, 6, 1), 177000),
('Adams', 'Accounting', 50000, datetime.date(2013, 7, 1), 177000),
('Wilkinson', 'IT', 60000, datetime.date(2011, 3, 1), 94000),
('Moore', 'IT', 34000, datetime.date(2013, 8, 1), 94000),
('Miller', 'Management', 100000, datetime.date(2005, 6, 1), 180000),
('Johnson', 'Management', 80000, datetime.date(2005, 7, 1), 180000),
('Smith', 'Marketing', 38000, datetime.date(2009, 10, 1), 78000),
('Johnson', 'Marketing', 40000, datetime.date(2012, 3, 1), 78000),
('Smith', 'Sales', 55000, datetime.date(2007, 6, 1), 108000),
('Brown', 'Sales', 53000, datetime.date(2009, 9, 1), 108000),
], transform=lambda row: (row.name, row.department, row.salary, row.hire_date, row.sum))
示例8: test_distinct_window_function
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def test_distinct_window_function(self):
"""
Window functions are not aggregates, and hence a query to filter out
duplicates may be useful.
"""
qs = Employee.objects.annotate(
sum=Window(
expression=Sum('salary'),
partition_by=ExtractYear('hire_date'),
order_by=ExtractYear('hire_date')
),
year=ExtractYear('hire_date'),
).values('year', 'sum').distinct('year').order_by('year')
results = [
{'year': 2005, 'sum': 225000}, {'year': 2007, 'sum': 55000},
{'year': 2008, 'sum': 45000}, {'year': 2009, 'sum': 128000},
{'year': 2011, 'sum': 60000}, {'year': 2012, 'sum': 40000},
{'year': 2013, 'sum': 84000},
]
for idx, val in zip(range(len(results)), results):
with self.subTest(result=val):
self.assertEqual(qs[idx], val)
示例9: check_expression_support
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def check_expression_support(self, expression):
bad_fields = (fields.DateField, fields.DateTimeField, fields.TimeField)
bad_aggregates = (aggregates.Sum, aggregates.Avg, aggregates.Variance, aggregates.StdDev)
if isinstance(expression, bad_aggregates):
try:
output_field = expression.input_field.output_field
if isinstance(output_field, bad_fields):
raise NotImplementedError(
'You cannot use Sum, Avg, StdDev and Variance aggregations '
'on date/time fields in sqlite3 '
'since date/time is saved as text.')
except FieldError:
# not every sub-expression has an output_field which is fine to
# ignore
pass
示例10: total_impressions_for_period
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def total_impressions_for_period(self, start, finish):
return self.within_period(start, finish).aggregate(Sum('impressions'))
示例11: daily_impressions_for_period
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def daily_impressions_for_period(self, start, finish, order_by='day'):
return self.within_period(start, finish).order_by(order_by).values('day').annotate(impressions=Sum('impressions'))
示例12: total_clicks_for_period
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def total_clicks_for_period(self, start, finish):
return self.within_period(start, finish).aggregate(Sum('clicks'))
示例13: daily_clicks_for_period
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def daily_clicks_for_period(self, start, finish, order_by='day'):
return self.within_period(start, finish).order_by(order_by).values('day').annotate(clicks=Sum('clicks'))
示例14: daily_cost_for_period
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def daily_cost_for_period(self, start, finish, order_by='day'):
return self.within_period(start, finish).order_by(order_by).values('day').annotate(cost=Sum('cost'))
示例15: total_conversions_for_period
# 需要導入模塊: from django.db.models import aggregates [as 別名]
# 或者: from django.db.models.aggregates import Sum [as 別名]
def total_conversions_for_period(self, start, finish):
return self.within_period(start, finish).aggregate(Sum('conversions'))