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.2.tar.gz (29.9 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.2-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_returns-0.2.2.tar.gz
  • Upload date:
  • Size: 29.9 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.2.tar.gz
Algorithm Hash digest
SHA256 f26d273e29b25affd33b324aa70139430cf491fdeadd79a579666b157ab4b1c6
MD5 2314c06156552b57a87c5b90b53cb124
BLAKE2b-256 29140fbd2024fd3ea6b44d5913ae0dd0b8fad33c577c042496d6b6aea5045d62

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: django_returns-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 19.6 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3cd019bb9ff13df62e0c8f91bc2d5792a5fa3cefc74bb512b575c8d232af31d4
MD5 474608bfbfbb309a20f51c9642f827ec
BLAKE2b-256 ce61d4756a4b2599b0b6a0dbf63bcd13b70ec8c2092f1aac2a340bed8dc21acb

See more details on using hashes here.

Provenance

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