Skip to main content

Meaningful, typed, safe Django utils

Project description

django-returns

CI PyPI Python Versions License

Meaningful Django utils based on Functional Programming.

Made possible by 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.

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:

  • *_safe variants for exception-raising operations (return Success / Failure)
  • first_maybe() / last_maybe() for “might be empty” queries (return Some / Nothing)
  • async *_safe variants which return an IO-wrapped Result and can be unwrapped with django_returns.utils.unsafe_perform_io

Opt-in Safety With *_safe Suffix

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 _safe suffix return returns.result.Result.

from returns.result import Failure, Success

from .models import Person


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

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

Async Methods

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

from django_returns.utils import unsafe_perform_io
from returns.result import Failure, Success

from .models import Person


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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_returns-0.1.1.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_returns-0.1.1-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file django_returns-0.1.1.tar.gz.

File metadata

  • Download URL: django_returns-0.1.1.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_returns-0.1.1.tar.gz
Algorithm Hash digest
SHA256 843e0ebb248ee4343aa7a8a0992c079616c4f86b962ff45eb4ab938495b741d9
MD5 bfae2f974e667087b8a5b7a94a891cb3
BLAKE2b-256 1f6526059579fa5abb0e10dc585f9d6b88608e234121177416a02009db336f37

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_returns-0.1.1.tar.gz:

Publisher: publish.yml on brunodantas/django-returns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_returns-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_returns-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_returns-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f3641c4667e18f01edc34ffe67921d94e5b8d17b89c71809aea20bd6acddeae5
MD5 be51df77731dd88d02798bb8ae5cbce5
BLAKE2b-256 4e161be00cb3d933758512703413f466f22743ae5a26759e01ad60fefd99d217

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_returns-0.1.1-py3-none-any.whl:

Publisher: publish.yml on brunodantas/django-returns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page