当前位置: 首页>>代码示例>>Python>>正文


Python expressions.Func方法代码示例

本文整理汇总了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 
开发者ID:nesdis,项目名称:djongo,代码行数:25,代码来源:test_gis_tests_utils.py

示例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) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:13,代码来源:functions.py

示例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 
开发者ID:project-koku,项目名称:koku,代码行数:41,代码来源:query_handler.py


注:本文中的django.db.models.expressions.Func方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。