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示例來說它很重要。
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
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)
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)
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
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
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。