本文整理汇总了Python中django.db.models.expressions.Func方法的典型用法代码示例。如果您正苦于以下问题:Python expressions.Func方法的具体用法?Python expressions.Func怎么用?Python expressions.Func使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.expressions
的用法示例。
在下文中一共展示了expressions.Func方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mutation
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Func [as 别名]
def test_mutation(raises=True):
def wrapper(mutation_func):
def test(test_case_instance, *args, **kwargs):
class TestFunc(Func):
output_field = models.IntegerField()
def __init__(self):
self.attribute = 'initial'
super().__init__('initial', ['initial'])
def as_sql(self, *args, **kwargs):
mutation_func(self)
return '', ()
if raises:
msg = 'TestFunc Func was mutated during compilation.'
with test_case_instance.assertRaisesMessage(AssertionError, msg):
getattr(TestFunc(), 'as_' + connection.vendor)(None, None)
else:
getattr(TestFunc(), 'as_' + connection.vendor)(None, None)
return test
return wrapper
示例2: as_oracle
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Func [as 别名]
def as_oracle(self, compiler, connection):
# we can't mix TextField (NCLOB) and CharField (NVARCHAR), so convert
# all fields to NCLOB when we expect NCLOB
if self.output_field.get_internal_type() == 'TextField':
class ToNCLOB(Func):
function = 'TO_NCLOB'
expressions = [
ToNCLOB(expression) for expression in self.get_source_expressions()]
self.set_source_expressions(expressions)
return super(Coalesce, self).as_sql(compiler, connection)
示例3: calculate_total
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Func [as 别名]
def calculate_total(self, **units):
"""Calculate aggregated totals for the query.
Args:
units (dict): The units dictionary
Returns:
(dict) The aggregated totals for the query
"""
query_group_by = ["date"] + self._get_group_by()
query = self.query_table.objects.filter(self.query_filter)
query_data = query.annotate(**self.annotations)
query_data = query_data.values(*query_group_by)
aggregates = copy.deepcopy(self._mapper.report_type_map.get("aggregates", {}))
if not self.parameters.parameters.get("compute_count"):
# Query parameter indicates count should be removed from DB queries
aggregates.pop("count", None)
counts = None
if "count" in aggregates:
resource_ids = (
query_data.annotate(resource_id=Func(F("resource_ids"), function="unnest"))
.values_list("resource_id", flat=True)
.distinct()
)
counts = len(resource_ids)
total_query = query.aggregate(**aggregates)
for unit_key, unit_value in units.items():
total_query[unit_key] = unit_value
if counts:
total_query["count"] = counts
self._pack_data_object(total_query, **self._mapper.PACK_DEFINITIONS)
return total_query