鍍金池/ 教程/ Python/ 數(shù)據(jù)庫函數(shù)
點擊劫持保護
安全問題歸檔
Model 類參考
將遺留數(shù)據(jù)庫整合到Django
關聯(lián)對象參考
內(nèi)建基于類的視圖的API
聚合
Django 中的用戶認證
django.contrib.humanize
Django管理文檔生成器
分頁
使用Django輸出CSV
加密簽名
文件儲存API
安全
Django中的測試
國際化和本地化
為Django編寫首個補丁
條件表達式
日志
模型元選項
部署靜態(tài)文件
執(zhí)行查詢
使用Django認證系統(tǒng)
基于類的視圖
中間件
編寫自定義的django-admin命令
Django 的設置
格式本地化
數(shù)據(jù)庫訪問優(yōu)化
錯誤報告
基于類的內(nèi)建通用視圖
編寫自定義存儲系統(tǒng)
編寫你的第一個 Django 程序 第3部分
編寫數(shù)據(jù)庫遷移
使用表單
編寫你的第一個 Django 程序 第2部分
編寫你的第一個 Django 程序 第1部分
如何使用會話
系統(tǒng)檢查框架
新手入門
信號
編寫視圖
如何使用WSGI 部署
編寫你的第一個Django應用,第6部分
常見的網(wǎng)站應用工具
Widgets
內(nèi)建的視圖
模型實例參考
視圖層
Django中的密碼管理
高級教程:如何編寫可重用的應用
國際化和本地化
"本地特色"附加功能
TemplateResponse 和 SimpleTemplateResponse
模式編輯器
文件上傳
快速安裝指南
部署 Django
表單 API
表單素材 ( <code>Media</code> 類)
管理文件
其它核心功能
查找 API 參考
表單
Admin
數(shù)據(jù)庫函數(shù)
自定義查找
使用基于類的視圖處理表單
管理操作
開發(fā)過程
編寫你的第一個Django應用,第5部分
進行原始的sql查詢
模型層
多數(shù)據(jù)庫
編寫你的第一個 Django 程序 第4部分
Django安全
Django 初探
Django異常
重定向應用
按需內(nèi)容處理
管理器
視圖裝飾器
驗證器
使用Django輸出PDF
File對象
Django 的快捷函數(shù)
基于類的通用視圖 —— 索引
為模型提供初始數(shù)據(jù)
模板層
URL調(diào)度器
中間件
模型

數(shù)據(jù)庫函數(shù)

New in Django 1.8.

下面記述的類為用戶提供了一些方法,來在Django中使用底層數(shù)據(jù)庫提供的函數(shù)用于注解、聚合或者過濾器等操作。函數(shù)也是表達式,所以可以像聚合函數(shù)一樣混合使用它們。

我們會在每個函數(shù)的實例中使用下面的模型:

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]

接受一個含有至少兩個字段名稱或表達式的列表,返回第一個非空的值(注意空字符串不被認為是一個空值)。每個參與都必須是相似的類型,所以摻雜了文本和數(shù)字的列表會導致數(shù)據(jù)庫錯誤。

使用范例:

>>> # 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]

接受一個含有至少兩個文本字段的或表達式的列表,返回連接后的文本。每個參數(shù)都必須是文本或者字符類型。如果你想把一個TextField()和一個CharField()連接, 一定要告訴Djangooutput_field應該為TextField()類型。在下面連接Value的例子中,這也是必需的。

這個函數(shù)不會返回null。在后端中,如果一個null參數(shù)導致了整個表達式都是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]

接受一個文本字段或表達式,返回值的字符個數(shù)。如果表達式是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。如果lengthNone,會返回剩余的字符串。

使用范例:

>>> # 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 文檔協(xié)作翻譯小組,原文:Database Functions。

本文以 CC BY-NC-SA 3.0 協(xié)議發(fā)布,轉載請保留作者署名和文章出處。

Django 文檔協(xié)作翻譯小組人手緊缺,有興趣的朋友可以加入我們,完全公益性質。交流群:467338606。