“Django/docs/3.0.x/ref/contrib/postgres/functions”的版本间差异

来自菜鸟教程
Django/docs/3.0.x/ref/contrib/postgres/functions
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:PostgreSQL 特定的数据库函数 — Django 文档}}
 
<div id="postgresql-specific-database-functions" class="section">
 
<div id="postgresql-specific-database-functions" class="section">
  
= PostgreSQL specific database functions =
+
= PostgreSQL 特有数据库函数 =
  
All of these functions are available from the
+
所有这些功能都可以从 <code>django.contrib.postgres.functions</code> 模块获得。
<code>django.contrib.postgres.functions</code> module.
 
  
 
<div id="randomuuid" class="section">
 
<div id="randomuuid" class="section">
  
== <code>RandomUUID</code> ==
+
== RandomUUID ==
  
; ''class'' <code>RandomUUID</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">RandomUUID</span></span>
 
:  
 
:  
  
Returns a version 4 UUID.
+
返回一个版本 4 的 UUID。
  
The [https://www.postgresql.org/docs/current/pgcrypto.html pgcrypto extension] must be installed. You can use the
+
必须安装 [https://www.postgresql.org/docs/current/pgcrypto.html pgcrypto 扩展] 。 您可以使用 [[../operations#django.contrib.postgres.operations|CryptoExtension]] 迁移操作来安装它。
[[../operations#django.contrib.postgres.operations|<code>CryptoExtension</code>]] migration
 
operation to install it.
 
  
使用实例:
+
用法示例:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第25行: 第23行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; from django.contrib.postgres.functions import RandomUUID
+
<syntaxhighlight lang="python">>>> from django.contrib.postgres.functions import RandomUUID
&gt;&gt;&gt; Article.objects.update(uuid=RandomUUID())</pre>
+
>>> Article.objects.update(uuid=RandomUUID())</syntaxhighlight>
  
 
</div>
 
</div>
第35行: 第33行:
 
<div id="transactionnow" class="section">
 
<div id="transactionnow" class="section">
  
== <code>TransactionNow</code> ==
+
== TransactionNow ==
  
; ''class'' <code>TransactionNow</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">TransactionNow</span></span>
 
:  
 
:  
  
Returns the date and time on the database server that the current transaction
+
返回数据库服务器上当前事务开始的日期和时间。 如果您不在交易中,它将返回当前报表的日期和时间。 这是对 [[../../../models/database-functions#django.db.models.functions|django.db.models.functions.Now]] 的补充,它返回当前语句的日期和时间。
started. If you are not in a transaction it will return the date and time of
 
the current statement. This is a complement to
 
[[../../../models/database-functions#django.db.models.functions|<code>django.db.models.functions.Now</code>]], which returns the date and time of the
 
current statement.
 
  
Note that only the outermost call to [[../../../../topics/db/transactions#django.db.transaction|<code>atomic()</code>]]
+
请注意,只有对 [[../../../../topics/db/transactions#django.db.transaction|atomic()]] 的最外层调用设置了一个事务,从而设置了 <code>TransactionNow()</code> 将返回的时间; 嵌套调用创建不影响事务时间的保存点。
sets up a transaction and thus sets the time that <code>TransactionNow()</code> will
 
return; nested calls create savepoints which do not affect the transaction
 
time.
 
  
使用实例:
+
用法示例:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第57行: 第48行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; from django.contrib.postgres.functions import TransactionNow
+
<syntaxhighlight lang="python">>>> from django.contrib.postgres.functions import TransactionNow
&gt;&gt;&gt; Article.objects.filter(published__lte=TransactionNow())
+
>>> Article.objects.filter(published__lte=TransactionNow())
&lt;QuerySet [&lt;Article: How to Django&gt;]&gt;</pre>
+
<QuerySet [<Article: How to Django>]></syntaxhighlight>
  
 
</div>
 
</div>
第68行: 第59行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Django 3.0.x 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Django 3.0.x 文档]]

2021年10月31日 (日) 04:09的最新版本

PostgreSQL 特有数据库函数

所有这些功能都可以从 django.contrib.postgres.functions 模块获得。

RandomUUID

class RandomUUID

返回一个版本 4 的 UUID。

必须安装 pgcrypto 扩展 。 您可以使用 CryptoExtension 迁移操作来安装它。

用法示例:

>>> from django.contrib.postgres.functions import RandomUUID
>>> Article.objects.update(uuid=RandomUUID())

TransactionNow

class TransactionNow

返回数据库服务器上当前事务开始的日期和时间。 如果您不在交易中,它将返回当前报表的日期和时间。 这是对 django.db.models.functions.Now 的补充,它返回当前语句的日期和时间。

请注意,只有对 atomic() 的最外层调用设置了一个事务,从而设置了 TransactionNow() 将返回的时间; 嵌套调用创建不影响事务时间的保存点。

用法示例:

>>> from django.contrib.postgres.functions import TransactionNow
>>> Article.objects.filter(published__lte=TransactionNow())
<QuerySet [<Article: How to Django>]>