Meaningful, typed, safe Django utils
Project description
django-returns
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:
*_safevariants for exception-raising operations (returnSuccess/Failure)first_maybe()/last_maybe()for “might be empty” queries (returnSome/Nothing)- async
*_safevariants which return an IO-wrappedResultand can be unwrapped withdjango_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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
843e0ebb248ee4343aa7a8a0992c079616c4f86b962ff45eb4ab938495b741d9
|
|
| MD5 |
bfae2f974e667087b8a5b7a94a891cb3
|
|
| BLAKE2b-256 |
1f6526059579fa5abb0e10dc585f9d6b88608e234121177416a02009db336f37
|
Provenance
The following attestation bundles were made for django_returns-0.1.1.tar.gz:
Publisher:
publish.yml on brunodantas/django-returns
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_returns-0.1.1.tar.gz -
Subject digest:
843e0ebb248ee4343aa7a8a0992c079616c4f86b962ff45eb4ab938495b741d9 - Sigstore transparency entry: 832912783
- Sigstore integration time:
-
Permalink:
brunodantas/django-returns@c1658db0942140b5e1dc48498c998344919b436e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/brunodantas
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c1658db0942140b5e1dc48498c998344919b436e -
Trigger Event:
workflow_run
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3641c4667e18f01edc34ffe67921d94e5b8d17b89c71809aea20bd6acddeae5
|
|
| MD5 |
be51df77731dd88d02798bb8ae5cbce5
|
|
| BLAKE2b-256 |
4e161be00cb3d933758512703413f466f22743ae5a26759e01ad60fefd99d217
|
Provenance
The following attestation bundles were made for django_returns-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on brunodantas/django-returns
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_returns-0.1.1-py3-none-any.whl -
Subject digest:
f3641c4667e18f01edc34ffe67921d94e5b8d17b89c71809aea20bd6acddeae5 - Sigstore transparency entry: 832912787
- Sigstore integration time:
-
Permalink:
brunodantas/django-returns@c1658db0942140b5e1dc48498c998344919b436e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/brunodantas
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c1658db0942140b5e1dc48498c998344919b436e -
Trigger Event:
workflow_run
-
Statement type: