<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 條件表達式 # ``` New in Django 1.8. ``` 條件表達式允許你在過濾器、注解、聚合和更新操作中使用 `if ... elif ... else`的邏輯。條件[表達式](http://python.usyiyi.cn/django/ref/models/expressions.html)為表中的每一行計算一系列的條件,并且返回匹配到的結果表達式。條件表達式也可以像其它 表達式一樣混合和嵌套。 ## 條件表達式類 ## 我們會在后面的例子中使用下面的模型: ``` from django.db import models class Client(models.Model): REGULAR = 'R' GOLD = 'G' PLATINUM = 'P' ACCOUNT_TYPE_CHOICES = ( (REGULAR, 'Regular'), (GOLD, 'Gold'), (PLATINUM, 'Platinum'), ) name = models.CharField(max_length=50) registered_on = models.DateField() account_type = models.CharField( max_length=1, choices=ACCOUNT_TYPE_CHOICES, default=REGULAR, ) ``` ### When ### `class When(condition=None, then=None, **lookups)[source]` `When()`對象用于封裝條件和它的結果,為了在條件表達式中使用。使用`When()`對象和使用`filter()` 方法類似。條件可以使用[字段查找](http://python.usyiyi.cn/django/ref/models/querysets.html#field-lookups) 或者 `Q` 來指定。結果通過使用`then`關鍵字來提供。 一些例子: ``` >>> from django.db.models import When, F, Q >>> # String arguments refer to fields; the following two examples are equivalent: >>> When(account_type=Client.GOLD, then='name') >>> When(account_type=Client.GOLD, then=F('name')) >>> # You can use field lookups in the condition >>> from datetime import date >>> When(registered_on__gt=date(2014, 1, 1), ... registered_on__lt=date(2015, 1, 1), ... then='account_type') >>> # Complex conditions can be created using Q objects >>> When(Q(name__startswith="John") | Q(name__startswith="Paul"), ... then='name') ``` 要注意這些值中的每一個都可以是表達式。 > 注意 > > 由于then 關鍵字參數為 When()的結果而保留,如果Model有名稱為 then的字段,會有潛在的沖突。這可以用以下兩種辦法解決: ``` >>> from django.db.models import Value >>> When(then__exact=0, then=1) >>> When(Q(then=0), then=1) ``` ### Case ### `class Case(*cases, **extra)[source]` `Case()`表達式就像是Python中的`if ... elif ... else`語句。每個提供的`When()`中的`condition` 按照順序計算,直到得到一個真值。返回匹配`When() `對象的`result`表達式。 一個簡單的例子: ``` >>> >>> from datetime import date, timedelta >>> from django.db.models import CharField, Case, Value, When >>> Client.objects.create( ... name='Jane Doe', ... account_type=Client.REGULAR, ... registered_on=date.today() - timedelta(days=36)) >>> Client.objects.create( ... name='James Smith', ... account_type=Client.GOLD, ... registered_on=date.today() - timedelta(days=5)) >>> Client.objects.create( ... name='Jack Black', ... account_type=Client.PLATINUM, ... registered_on=date.today() - timedelta(days=10 * 365)) >>> # Get the discount for each Client based on the account type >>> Client.objects.annotate( ... discount=Case( ... When(account_type=Client.GOLD, then=Value('5%')), ... When(account_type=Client.PLATINUM, then=Value('10%')), ... default=Value('0%'), ... output_field=CharField(), ... ), ... ).values_list('name', 'discount') [('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')] ``` `Case()` 接受任意數量的`When()`對象作為獨立的參數。其它選項使用關鍵字參數提供。如果沒有條件為`TRUE`,表達式會返回提供的`default`關鍵字參數。如果沒有提供`default`參數,會使用`Value(None)`。 如果我們想要修改之前的查詢,來獲取基于`Client`跟著我們多長時間的折扣,我們應該這樣使用查找: ``` >>> a_month_ago = date.today() - timedelta(days=30) >>> a_year_ago = date.today() - timedelta(days=365) >>> # Get the discount for each Client based on the registration date >>> Client.objects.annotate( ... discount=Case( ... When(registered_on__lte=a_year_ago, then=Value('10%')), ... When(registered_on__lte=a_month_ago, then=Value('5%')), ... default=Value('0%'), ... output_field=CharField(), ... ) ... ).values_list('name', 'discount') [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')] ``` > 注意 > > 記住條件按照順序來計算,所以上面的例子中,即使第二個條件匹配到了 Jane Doe 和 Jack Black,我們也得到了正確的結果。這就像Python中的if ... elif ... else語句一樣。 ## 高級查詢 ## 條件表達式可以用于注解、聚合、查找和更新。它們也可以和其它表達式混合和嵌套。這可以讓你構造更強大的條件查詢。 ### 條件更新 ### 假設我們想要為客戶端修改`account_type`來匹配它們的注冊日期。我們可以使用條件表達式和`update()`放啊來實現: ``` >>> a_month_ago = date.today() - timedelta(days=30) >>> a_year_ago = date.today() - timedelta(days=365) >>> # Update the account_type for each Client from the registration date >>> Client.objects.update( ... account_type=Case( ... When(registered_on__lte=a_year_ago, ... then=Value(Client.PLATINUM)), ... When(registered_on__lte=a_month_ago, ... then=Value(Client.GOLD)), ... default=Value(Client.REGULAR) ... ), ... ) >>> Client.objects.values_list('name', 'account_type') [('Jane Doe', 'G'), ('James Smith', 'R'), ('Jack Black', 'P')] ``` ### 條件聚合 ### 如果我們想要弄清楚每個`account_type`有多少客戶端,要怎么做呢?我們可以在[聚合函數](http://python.usyiyi.cn/django/ref/models/querysets.html#aggregation-functions)中嵌套條件表達式來實現: ``` >>> # Create some more Clients first so we can have something to count >>> Client.objects.create( ... name='Jean Grey', ... account_type=Client.REGULAR, ... registered_on=date.today()) >>> Client.objects.create( ... name='James Bond', ... account_type=Client.PLATINUM, ... registered_on=date.today()) >>> Client.objects.create( ... name='Jane Porter', ... account_type=Client.PLATINUM, ... registered_on=date.today()) >>> # Get counts for each value of account_type >>> from django.db.models import IntegerField, Sum >>> Client.objects.aggregate( ... regular=Sum( ... Case(When(account_type=Client.REGULAR, then=1), ... output_field=IntegerField()) ... ), ... gold=Sum( ... Case(When(account_type=Client.GOLD, then=1), ... output_field=IntegerField()) ... ), ... platinum=Sum( ... Case(When(account_type=Client.PLATINUM, then=1), ... output_field=IntegerField()) ... ) ... ) {'regular': 2, 'gold': 1, 'platinum': 3} ``` > 譯者:[Django 文檔協作翻譯小組](http://python.usyiyi.cn/django/index.html),原文:[Conditional Expressions](https://docs.djangoproject.com/en/1.8/ref/models/conditional-expressions/)。 > > 本文以 [CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/cn/) 協議發布,轉載請保留作者署名和文章出處。 > > [Django 文檔協作翻譯小組](http://python.usyiyi.cn/django/index.html)人手緊缺,有興趣的朋友可以加入我們,完全公益性質。交流群:467338606。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看