Skip to main content

Meaningful Django utils based on Functional Programming

Project description

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

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.1.tar.gz (29.7 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.1-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_returns-0.2.1.tar.gz
  • Upload date:
  • Size: 29.7 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.1.tar.gz
Algorithm Hash digest
SHA256 a505442407508b1133dfb205a4239f8308056b036e6e4865174bf2e816b4f087
MD5 8b1ced61d8facaf5eedc9a4bd4f233d3
BLAKE2b-256 82f95e67a39ef1bcfe26c9f9dfc452f54da430c00eb758a77fd1a93320b1af48

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: django_returns-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 19.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c422b7e56b0367b1b47d5f9d78dffeeb541e175d8b9348bea6f9dd13db41fc8
MD5 c41e2b412d07461cf2aae051265b569b
BLAKE2b-256 aea8dc73d9868ce7a6100246be91aedb16db5ac93ede6fe31bdafc5467abbc2d

See more details on using hashes here.

Provenance

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