當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。