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


Python expressions.Case方法代码示例

本文整理汇总了Python中django.db.models.expressions.Case方法的典型用法代码示例。如果您正苦于以下问题:Python expressions.Case方法的具体用法?Python expressions.Case怎么用?Python expressions.Case使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.db.models.expressions的用法示例。


在下文中一共展示了expressions.Case方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: as_sql

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def as_sql(self, compiler, connection, **extra_context):
        if self.filter:
            if connection.features.supports_aggregate_filter_clause:
                filter_sql, filter_params = self.filter.as_sql(compiler, connection)
                template = self.filter_template % extra_context.get('template', self.template)
                sql, params = super().as_sql(compiler, connection, template=template, filter=filter_sql)
                return sql, params + filter_params
            else:
                copy = self.copy()
                copy.filter = None
                condition = When(Q())
                source_expressions = copy.get_source_expressions()
                condition.set_source_expressions([self.filter, source_expressions[0]])
                copy.set_source_expressions([Case(condition)] + source_expressions[1:])
                return super(Aggregate, copy).as_sql(compiler, connection, **extra_context)
        return super().as_sql(compiler, connection, **extra_context) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:18,代码来源:aggregates.py

示例2: test_annotate_with_annotation_in_predicate

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_annotate_with_annotation_in_predicate(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(
                f_minus_2=F('integer') - 2,
            ).annotate(
                test=Case(
                    When(f_minus_2=-1, then=Value('negative one')),
                    When(f_minus_2=0, then=Value('zero')),
                    When(f_minus_2=1, then=Value('one')),
                    default=Value('other'),
                    output_field=models.CharField(),
                ),
            ).order_by('pk'),
            [(1, 'negative one'), (2, 'zero'), (3, 'one'), (2, 'zero'), (3, 'one'), (3, 'one'), (4, 'other')],
            transform=attrgetter('integer', 'test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:18,代码来源:tests.py

示例3: test_annotate_with_aggregation_in_predicate

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_annotate_with_aggregation_in_predicate(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.values(*self.non_lob_fields).annotate(
                max=Max('fk_rel__integer'),
            ).annotate(
                test=Case(
                    When(max=3, then=Value('max = 3')),
                    When(max=4, then=Value('max = 4')),
                    default=Value(''),
                    output_field=models.CharField(),
                ),
            ).order_by('pk'),
            [(1, 1, ''), (2, 3, 'max = 3'), (3, 4, 'max = 4'), (2, 3, 'max = 3'),
             (3, 4, 'max = 4'), (3, 4, 'max = 4'), (4, 5, '')],
            transform=itemgetter('integer', 'max', 'test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:18,代码来源:tests.py

示例4: test_aggregate

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_aggregate(self):
        self.assertEqual(
            CaseTestModel.objects.aggregate(
                one=models.Sum(Case(
                    When(integer=1, then=1),
                    output_field=models.IntegerField(),
                )),
                two=models.Sum(Case(
                    When(integer=2, then=1),
                    output_field=models.IntegerField(),
                )),
                three=models.Sum(Case(
                    When(integer=3, then=1),
                    output_field=models.IntegerField(),
                )),
                four=models.Sum(Case(
                    When(integer=4, then=1),
                    output_field=models.IntegerField(),
                )),
            ),
            {'one': 1, 'two': 2, 'three': 3, 'four': 1}
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:24,代码来源:tests.py

示例5: test_update_decimal

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_update_decimal(self):
        CaseTestModel.objects.update(
            decimal=Case(
                When(integer=1, then=Decimal('1.1')),
                When(integer=2, then=Decimal('2.2')),
            ),
        )
        self.assertQuerysetEqual(
            CaseTestModel.objects.all().order_by('pk'),
            [
                (1, Decimal('1.1')),
                (2, Decimal('2.2')),
                (3, None),
                (2, Decimal('2.2')),
                (3, None),
                (3, None),
                (4, None)
            ],
            transform=attrgetter('integer', 'decimal')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:22,代码来源:tests.py

示例6: test_update_url

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_update_url(self):
        CaseTestModel.objects.update(
            url=Case(
                When(integer=1, then=Value('http://1.example.com/')),
                When(integer=2, then=Value('http://2.example.com/')),
                default=Value(''),
            ),
        )
        self.assertQuerysetEqual(
            CaseTestModel.objects.all().order_by('pk'),
            [
                (1, 'http://1.example.com/'), (2, 'http://2.example.com/'), (3, ''), (2, 'http://2.example.com/'),
                (3, ''), (3, ''), (4, '')
            ],
            transform=attrgetter('integer', 'url')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:18,代码来源:tests.py

示例7: test_lookup_in_condition

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_lookup_in_condition(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(
                test=Case(
                    When(integer__lt=2, then=Value('less than 2')),
                    When(integer__gt=2, then=Value('greater than 2')),
                    default=Value('equal to 2'),
                    output_field=models.CharField(),
                ),
            ).order_by('pk'),
            [
                (1, 'less than 2'), (2, 'equal to 2'), (3, 'greater than 2'), (2, 'equal to 2'), (3, 'greater than 2'),
                (3, 'greater than 2'), (4, 'greater than 2')
            ],
            transform=attrgetter('integer', 'test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:18,代码来源:tests.py

示例8: test_m2m_exclude

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_m2m_exclude(self):
        CaseTestModel.objects.create(integer=10, integer2=1, string='1')
        qs = CaseTestModel.objects.values_list('id', 'integer').annotate(
            cnt=models.Sum(
                Case(When(~Q(fk_rel__integer=1), then=1), default=2),
                output_field=models.IntegerField()
            ),
        ).order_by('integer')
        # The first o has 2 as its fk_rel__integer=1, thus it hits the
        # default=2 case. The other ones have 2 as the result as they have 2
        # fk_rel objects, except for integer=4 and integer=10 (created above).
        # The integer=4 case has one integer, thus the result is 1, and
        # integer=10 doesn't have any and this too generates 1 (instead of 0)
        # as ~Q() also matches nulls.
        self.assertQuerysetEqual(
            qs,
            [(1, 2), (2, 2), (2, 2), (3, 2), (3, 2), (3, 2), (4, 1), (10, 1)],
            lambda x: x[1:]
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:21,代码来源:tests.py

示例9: test_m2m_reuse

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_m2m_reuse(self):
        CaseTestModel.objects.create(integer=10, integer2=1, string='1')
        # Need to use values before annotate so that Oracle will not group
        # by fields it isn't capable of grouping by.
        qs = CaseTestModel.objects.values_list('id', 'integer').annotate(
            cnt=models.Sum(
                Case(When(~Q(fk_rel__integer=1), then=1), default=2),
                output_field=models.IntegerField()
            ),
        ).annotate(
            cnt2=models.Sum(
                Case(When(~Q(fk_rel__integer=1), then=1), default=2),
                output_field=models.IntegerField()
            ),
        ).order_by('integer')
        self.assertEqual(str(qs.query).count(' JOIN '), 1)
        self.assertQuerysetEqual(
            qs,
            [(1, 2, 2), (2, 2, 2), (2, 2, 2), (3, 2, 2), (3, 2, 2), (3, 2, 2), (4, 1, 1), (10, 1, 1)],
            lambda x: x[1:]
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:23,代码来源:tests.py

示例10: as_sql

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def as_sql(self, compiler, connection, **extra_context):
        if self.filter:
            if connection.features.supports_aggregate_filter_clause:
                filter_sql, filter_params = self.filter.as_sql(compiler, connection)
                template = self.filter_template % extra_context.get('template', self.template)
                sql, params = super().as_sql(compiler, connection, template=template, filter=filter_sql)
                return sql, params + filter_params
            else:
                copy = self.copy()
                copy.filter = None
                source_expressions = copy.get_source_expressions()
                condition = When(self.filter, then=source_expressions[0])
                copy.set_source_expressions([Case(condition)] + source_expressions[1:])
                return super(Aggregate, copy).as_sql(compiler, connection, **extra_context)
        return super().as_sql(compiler, connection, **extra_context) 
开发者ID:PacktPublishing,项目名称:Hands-On-Application-Development-with-PyCharm,代码行数:17,代码来源:aggregates.py

示例11: test_annotate

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_annotate(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(test=Case(
                When(integer=1, then=Value('one')),
                When(integer=2, then=Value('two')),
                default=Value('other'),
                output_field=models.CharField(),
            )).order_by('pk'),
            [(1, 'one'), (2, 'two'), (3, 'other'), (2, 'two'), (3, 'other'), (3, 'other'), (4, 'other')],
            transform=attrgetter('integer', 'test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:13,代码来源:tests.py

示例12: test_annotate_without_default

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_annotate_without_default(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(test=Case(
                When(integer=1, then=1),
                When(integer=2, then=2),
                output_field=models.IntegerField(),
            )).order_by('pk'),
            [(1, 1), (2, 2), (3, None), (2, 2), (3, None), (3, None), (4, None)],
            transform=attrgetter('integer', 'test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:12,代码来源:tests.py

示例13: test_annotate_with_expression_as_value

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_annotate_with_expression_as_value(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(f_test=Case(
                When(integer=1, then=F('integer') + 1),
                When(integer=2, then=F('integer') + 3),
                default='integer',
            )).order_by('pk'),
            [(1, 2), (2, 5), (3, 3), (2, 5), (3, 3), (3, 3), (4, 4)],
            transform=attrgetter('integer', 'f_test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:12,代码来源:tests.py

示例14: test_annotate_with_expression_as_condition

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_annotate_with_expression_as_condition(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(f_test=Case(
                When(integer2=F('integer'), then=Value('equal')),
                When(integer2=F('integer') + 1, then=Value('+1')),
                output_field=models.CharField(),
            )).order_by('pk'),
            [(1, 'equal'), (2, '+1'), (3, '+1'), (2, 'equal'), (3, '+1'), (3, 'equal'), (4, '+1')],
            transform=attrgetter('integer', 'f_test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:12,代码来源:tests.py

示例15: test_annotate_with_join_in_value

# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import Case [as 别名]
def test_annotate_with_join_in_value(self):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(join_test=Case(
                When(integer=1, then=F('o2o_rel__integer') + 1),
                When(integer=2, then=F('o2o_rel__integer') + 3),
                default='o2o_rel__integer',
            )).order_by('pk'),
            [(1, 2), (2, 5), (3, 3), (2, 5), (3, 3), (3, 3), (4, 1)],
            transform=attrgetter('integer', 'join_test')
        ) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:12,代码来源:tests.py


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