Skip to main content

Meaningful Django utils with returns

Project description

django-returns

CI PyPI Python Versions Django

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:

  • *_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)

Opt-in 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

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.2.0.tar.gz (29.4 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.2.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_returns-0.2.0.tar.gz
  • Upload date:
  • Size: 29.4 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.2.0.tar.gz
Algorithm Hash digest
SHA256 a0cf0580266da56884e76b14a3d043d9b7135a894be15ffa7a05c5be5e50dd66
MD5 fadb04d78fab66770f74f3e543db2dd1
BLAKE2b-256 aab352cbea2147dbb65fdf79890aa0ba0f686f17a6a74c4572f2eca8b7e3cf66

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_returns-0.2.0.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_returns-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.1 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cbe17afc99370be40fa958f08a868034138afd69003b5941dd8a14745641c8d0
MD5 94b717e5218d66e56f025fe2846f11e4
BLAKE2b-256 b05bbeb376eb56ab9db88f448c3e732d2d0e06f83c3c4a0cfa6f962d59b43ee3

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_returns-0.2.0-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