Skip to main content

django-returns

PyPI Python Versions Django Docs

Meaningful Django utils based on Functional Programming.

Made possible by returns

Install

pip install django-returns

What

django-returns is a tiny layer on top of Django’s ORM that lets you opt into returns containers when you want explicit success/failure return types instead of exceptions.

Why

To bring the benefits of Functional Programming to the Django world.

  • Improved predictability, understandability, maintainability.
  • Safer code with type checking.
  • Better error handling.

How

By subclassing QuerySet and applying returns decorators to its methods.

Note: the default ReturnsManager does not change Django semantics. It keeps the original methods intact and adds new ones:

  • *_result variants for sync operations (return Success / Failure)
  • *_ioresult variants for both sync and async operations (return IOSuccess / IOFailure)
  • first_maybe() / last_maybe() for "might be None" QS methods (return Some / Nothing)

Safe ORM

Enable it by using the provided base model.

from django_returns.models import ReturnsModel


class Person(ReturnsModel):
    name = models.CharField(max_length=255, unique=True)

Or by using the custom Manager.

from django_returns.managers import ReturnsManager


class Person(models.Model):
    objects = ReturnsManager()

Basic Usage

Methods with the _result suffix return returns.result.Result.

from returns.result import Failure, Result, Success


def get_person_name(person_id):
    result: Result = Person.objects.get_result(id=person_id)

    match result:
        case Success(person):
            return Success(person.name)
        case Failure(Person.DoesNotExist()):
            return ""

IO Methods

Async/IO *_ioresult methods return an IO-wrapped result (IOSuccess / IOFailure).

from returns.io import unsafe_perform_io
from returns.result import Failure, Success


async def get_person_id_async(name):
    io_result = await Person.objects.aget_ioresult(name=name)
    result = unsafe_perform_io(io_result)

    match result:
        case Success(person):
            return person.id
        case Failure(error):
            return -1

Metadata

Release files for django-returns 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-returns 0.2.2
File Size Uploaded
django_returns-0.2.2.tar.gz 29.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-returns 0.2.2
File Interpreter ABI Platform
django_returns-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 49.5 kB

Release files / django_returns-0.2.2.tar.gz

Download URL django_returns-0.2.2.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f26d273e29b25affd33b324aa70139430cf491fdeadd79a579666b157ab4b1c6
BLAKE2b-256 checksum
How to use checksums
29140fbd2024fd3ea6b44d5913ae0dd0b8fad33c577c042496d6b6aea5045d62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 23, 2026.

Transparency log

Release files / django_returns-0.2.2-py3-none-any.whl

Download URL django_returns-0.2.2-py3-none-any.whl
Size 19.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3cd019bb9ff13df62e0c8f91bc2d5792a5fa3cefc74bb512b575c8d232af31d4
BLAKE2b-256 checksum
How to use checksums
ce61d4756a4b2599b0b6a0dbf63bcd13b70ec8c2092f1aac2a340bed8dc21acb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 23, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page