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.0.tar.gz (4.5 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.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_returns-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.0

File hashes

Hashes for django_returns-0.1.0.tar.gz
Algorithm Hash digest
SHA256 88b61518bf7a3ffcd39212468b6616e9278e0df53cea35d0f9601a1d2efeeacc
MD5 85bbfe1726ec2297857286b7de41cb1c
BLAKE2b-256 09ace74c12c02b5fdd9b9d6c84716b97394fee8e56bb3b1c7bc70701c49a0728

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_returns-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6574077ace4a864122b96ae7fa554bdda9242576f841e8c526217689dc6a5d3c
MD5 a54aa15657d2ab831b25176e8ce360bb
BLAKE2b-256 e56ca5d3174647f1ba12797f45a5c63b559aef7168ce255ecd4fcee01a69269f

See more details on using hashes here.

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