當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Django Substr用法及代碼示例

本文介紹 django.db.models.functions.Substr 的用法。

聲明

class Substr(expression, pos, length=None, **extra)

從位置 pos 開始的字段或表達式返回長度為 length 的子字符串。該位置是 1 索引的,因此該位置必須大於 0。如果 lengthNone ,則將返回字符串的其餘部分。

使用示例:

>>> # Set the alias to the first 5 characters of the name as lowercase
>>> from django.db.models.functions import Lower, Substr
>>> 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

相關用法


注:本文由純淨天空篩選整理自djangoproject.com大神的英文原創作品 django.db.models.functions.Substr。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。