skillfed

django-pg-returning

A small library implementing PostgreSQL ability to return rows in DML statements for Django

django-pg-returning v2.0.2 178.5K downloads/30d#10,192 on PyPI18
Permissive license BSD 3-clause "New" or "Revised" License DORMANT released

What it is and what it does

django-pg-returning bridges a gap in Django's ORM: it exposes PostgreSQL's RETURNING clause for UPDATE, DELETE, and bulk CREATE operations. Normally, Django's update() and delete() methods don't return the modified rows, and bulk_create() doesn't refresh fields with database-computed defaults or trigger-set values. This library adds QuerySet methods (update_returning, delete_returning, create_returning, bulk_create_returning) and model methods (save_returning) that execute the DML statement and immediately fetch back the affected rows, including any values computed by the database.

The result is a ReturningQuerySet—a cached, in-memory collection similar to Django's RawQuerySet but with convenience methods like count(), first(), last(), values(), and values_list(). This is especially useful when you rely on database defaults, triggers, or F() expressions to compute field values and need to know what was actually stored without issuing a separate SELECT.

Use it for:

  • Retrieve auto-generated or trigger-computed values after bulk inserts without a follow-up query.
  • Update records with F() expressions and immediately see the evaluated result without reloading the instance.
  • Delete rows and capture their final state (e.g., for audit logging) in a single round-trip.
  • Bulk operations that depend on database defaults or computed columns to populate fields.
  • Workflows where you need to confirm what the database actually stored after a DML operation.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Extends Django's ORM to capture and return rows modified by PostgreSQL UPDATE, DELETE, and bulk CREATE operations, letting you retrieve database-computed values without extra queries.

Yes, if you use PostgreSQL and Django and frequently need to return modified rows from bulk operations or F() expressions. The library is stable and has no known vulnerabilities, but its dormant maintenance (last release 2024-01-04, no recent commits) means it may not support the latest Django versions—verify compatibility with your Django version before adopting. Low install friction and permissive licensing make it a low-risk experiment.

Install

django-pg-returning on PyPI

pip

pip install django-pg-returning

uv

uv add django-pg-returning

poetry

poetry add django-pg-returning

Installing django-pg-returning

Before you install

Low friction: pure Python wheel with only django and typing as runtime dependencies. Maintenance is dormant—last release 2024-01-04, no commits since 2024-09-19—so expect no active support or updates for new Django versions.

License in practice

BSD 3-clause permissive license; you may use, modify, and distribute freely with attribution and no warranty.

Quickstart

pip install django-pg-returning

from django_pg_returning import UpdateReturningModel
from django.db import models

class MyModel(UpdateReturningModel):
    field = models.IntegerField()

result = MyModel.objects.all().update_returning(field=1)
print(result.count())

Requires PostgreSQL 9.4+, psycopg2 or psycopg2-binary (not installed by this package), and Django 1.8+. Some features require Django 1.10+.

Verify before relying

  • Whether the library works with current Django versions (4.x, 5.x) given dormant maintenance status.
  • Compatibility with modern psycopg versions (3.x) and whether psycopg2 is still the expected adapter.
  • Performance characteristics when returning large result sets or with complex F() expressions.

Package facts

License BSD 3-clause "New" or "Revised" License (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies 2 — django, typing
Maintenance dormant — 953 days since the last release
Last repo commit
First released
Downloads 178,490/month — #10,192 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: django_pg_returning-2.0.2-py2.py3-none-any.whl

Tags

django postgresql returning clausedjango bulk operations with resultsdjango update delete returning rowspostgresql dml returning django ormdjango database triggers resultsdjango f expressions evaluated values
django-ormpostgresql-specific

More Database packages

psycopg2-binary

psycopg2-binary is a PostgreSQL database…

copyleft · top 1,000 on PyPI

redis

Python client library for connecting to and…

permissive · top 1,000 on PyPI

ydb

YDB Python SDK is the official client library…

permissive · top 1,000 on PyPI

snowflake-connector-python

Connects Python applications to Snowflake data…

permissive · top 1,000 on PyPI

sqlparse

sqlparse tokenizes SQL text into a tree of…

permissive · top 1,000 on PyPI

dbt-adapters

Provides base adapter protocols and shared…

permissive · top 1,000 on PyPI

django-pg-bulk-update

Executes bulk update and bulk create operations…

permissive · top 15,000 on PyPI

django-sql-utils

Provides simplified APIs for Django querysets…

permissive · top 15,000 on PyPI

djangoql

DjangoQL provides an advanced search query…

permissive · top 15,000 on PyPI

django-bulk-update

Performs bulk updates of Django ORM objects in…

permissive · top 15,000 on PyPI

django-postgres-copy

Provides Django ORM integration for…

permissive · top 5,000 on PyPI

drf-extra-fields

Provides specialized serializer fields for…

permissive · top 15,000 on PyPI

django-soft-delete

Adds soft deletion to Django models by marking…

permissive · top 15,000 on PyPI

modelsearch

Indexes Django models into Elasticsearch,…

permissive · top 15,000 on PyPI

django-chunkator

Iterates over large Django QuerySets in…

permissive · top 15,000 on PyPI

django-mock-queries

Mocks Django QuerySet operations in memory for…

permissive · top 15,000 on PyPI