<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. ``` 下面記述的類為用戶提供了一些方法,來在Django中使用底層數據庫提供的函數用于注解、聚合或者過濾器等操作。函數也是表達式,所以可以像聚合函數一樣混合使用它們。 我們會在每個函數的實例中使用下面的模型: ``` class Author(models.Model): name = models.CharField(max_length=50) age = models.PositiveIntegerField(null=True, blank=True) alias = models.CharField(max_length=50, null=True, blank=True) goes_by = models.CharField(max_length=50, null=True, blank=True) ``` 我們并不推薦在CharField上允許null=True,以后那位這會允許字段有兩個“空值”,但是對于下面的Coalesce示例來說它很重要。 ## Coalesce ## `class Coalesce(*expressions, **extra)[source]` 接受一個含有至少兩個字段名稱或表達式的列表,返回第一個非空的值(注意空字符串不被認為是一個空值)。每個參與都必須是相似的類型,所以摻雜了文本和數字的列表會導致數據庫錯誤。 使用范例: ``` >>> # Get a screen name from least to most public >>> from django.db.models import Sum, Value as V >>> from django.db.models.functions import Coalesce >>> Author.objects.create(name='Margaret Smith', goes_by='Maggie') >>> author = Author.objects.annotate( ... screen_name=Coalesce('alias', 'goes_by', 'name')).get() >>> print(author.screen_name) Maggie >>> # Prevent an aggregate Sum() from returning None >>> aggregated = Author.objects.aggregate( ... combined_age=Coalesce(Sum('age'), V(0)), ... combined_age_default=Sum('age')) >>> print(aggregated['combined_age']) 0 >>> print(aggregated['combined_age_default']) None ``` ## Concat ## `class Concat(*expressions, **extra)[source]` 接受一個含有至少兩個文本字段的或表達式的列表,返回連接后的文本。每個參數都必須是文本或者字符類型。如果你想把一個`TextField()`和一個`CharField()`連接, 一定要告訴Django`output_field`應該為`TextField()`類型。在下面連接`Value`的例子中,這也是必需的。 這個函數不會返回`null`。在后端中,如果一個`null`參數導致了整個表達式都是`null`,Django會確保把每個`null`的部分轉換成一個空字符串。 使用范例: ``` >>> # Get the display name as "name (goes_by)" >>> from django.db.models import CharField, Value as V >>> from django.db.models.functions import Concat >>> Author.objects.create(name='Margaret Smith', goes_by='Maggie') >>> author = Author.objects.annotate( ... screen_name=Concat('name', V(' ('), 'goes_by', V(')'), ... output_field=CharField())).get() >>> print(author.screen_name) Margaret Smith (Maggie) ``` ## Length ## `class Length(expression, **extra)[source]` 接受一個文本字段或表達式,返回值的字符個數。如果表達式是`null`,長度也會是`null`。 使用范例: ``` >>> # Get the length of the name and goes_by fields >>> from django.db.models.functions import Length >>> Author.objects.create(name='Margaret Smith') >>> author = Author.objects.annotate( ... name_length=Length('name'), ... goes_by_length=Length('goes_by')).get() >>> print(author.name_length, author.goes_by_length) (14, None) ``` ## Lower ## `class Lower(expression, **extra)[source]` 接受一個文本字符串或表達式,返回它的小寫表示形式。 使用范例: ``` >>> from django.db.models.functions import Lower >>> Author.objects.create(name='Margaret Smith') >>> author = Author.objects.annotate(name_lower=Lower('name')).get() >>> print(author.name_lower) margaret smith ``` ## Substr ## `class Substr(expression, pos, length=None, **extra)[source]` 返回這個字段或者表達式的,以`pos`位置開始,長度為`length`的子字符串。位置從下標為1開始,所以必須大于0。如果`length`是`None`,會返回剩余的字符串。 使用范例: ``` >>> # Set the alias to the first 5 characters of the name as lowercase >>> from django.db.models.functions import Substr, Lower >>> Author.objects.create(name='Margaret Smith') >>> Author.objects.update(alias=Lower(Substr('name', 1, 5))) 1 >>> print(Author.objects.get(name='Margaret Smith').alias) marga ``` ## Upper ## `class Upper(expression, **extra)[source]` 接受一個文本字符串或表達式,返回它的大寫表示形式。 使用范例: ``` >>> from django.db.models.functions import Upper >>> Author.objects.create(name='Margaret Smith') >>> author = Author.objects.annotate(name_upper=Upper('name')).get() >>> print(author.name_upper) MARGARET SMITH ``` > 譯者:[Django 文檔協作翻譯小組](http://python.usyiyi.cn/django/index.html),原文:[Database Functions](https://docs.djangoproject.com/en/1.8/ref/models/database-functions/)。 > > 本文以 [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>

                              哎呀哎呀视频在线观看