---
title: "PostgreSQL 特有数据库函数"
version: 6.1
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/6.1/ref/contrib/postgres/functions/
canonical: https://djangodocs.dev/zh-hans/6.1/ref/contrib/postgres/functions/
---
# PostgreSQL 特有数据库函数

所有这些函数都可以从 `django.contrib.postgres.function` 模块中获得。

## `RandomUUID`

#### `class RandomUUID`

返回一个版本 4 的 UUID。

用法示例：

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

> **Use django.db.models.functions.UUID4 instead.**
>
> [`django.db.models.functions.UUID4`](/zh-hans/6.1/ref/models/database-functions/#django.db.models.functions.UUID4) is a cross-database version of
> this function. `RandomUUID` may be deprecated in the future.

## `TransactionNow`

#### `class TransactionNow`

返回数据库服务器上当前事务开始的日期和时间。如果你不在事务中，它将返回当前语句的日期和时间。这是对 `django.db.models.function.Now` 的补充，后者返回当前语句的日期和时间。

Note that only the outermost call to [`atomic()`](/zh-hans/6.1/topics/db/transactions/#django.db.transaction.atomic)
sets up a transaction and thus sets the time that `TransactionNow()` will
return; nested calls create savepoints which do not affect the transaction
time.

用法示例：

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