A Django-like ORM with synchronous and asynchronous support
Project description
djanorm
A Django-inspired ORM for Python with full synchronous and asynchronous support. The same API you know from Django, without depending on the full framework.
Works with SQLite and PostgreSQL, and ships with a migration system, atomic transactions, signals, validation, relationship loading (select_related / prefetch_related), aggregations, DB functions and much more — all with real static typing (Field[T]).
Installation
# SQLite
pip install "djanorm[sqlite]"
# PostgreSQL
pip install "djanorm[postgresql]"
Quick start
1. Scaffold a project
dorm init blog
That creates:
settings.py— uncomment theDATABASESblock matching your backend.blog/— an app package with an emptymodels.py.
A minimal settings.py looks like:
DATABASES = {
"default": {
"ENGINE": "sqlite",
"NAME": "db.sqlite3",
},
}
INSTALLED_APPS = ["blog"]
2. Define a model
# blog/models.py
import dorm
class Author(dorm.Model):
name = dorm.CharField(max_length=100)
email = dorm.EmailField(unique=True)
is_active = dorm.BooleanField(default=True)
class Post(dorm.Model):
title = dorm.CharField(max_length=200)
body = dorm.TextField()
author = dorm.ForeignKey(Author, on_delete=dorm.CASCADE)
published_at = dorm.DateTimeField(null=True, blank=True)
class Meta:
ordering = ["-published_at"]
3. Generate and apply migrations
dorm makemigrations blog
dorm migrate
4. Use it
Open a shell with dorm shell (IPython auto-detected) or import
the models from your own script.
from blog.models import Author, Post
# Create
alice = Author.objects.create(name="Alice", email="alice@example.com")
post = Post.objects.create(
title="Hello world",
body="First post body.",
author=alice,
)
# Bulk create
Post.objects.bulk_create([
Post(title=f"Draft {i}", body="...", author=alice)
for i in range(5)
])
# Filter / exclude / Q / F
from dorm import Q, F
active_authors = Author.objects.filter(is_active=True)
some_posts = Post.objects.filter(
Q(title__icontains="hello") | Q(title__startswith="Draft")
).exclude(published_at__isnull=True)
# Lookups across relations
alices_posts = Post.objects.filter(author__name="Alice")
# select_related / prefetch_related to dodge N+1
for post in Post.objects.select_related("author"):
print(post.author.name, post.title) # 1 query, JOIN
# Get one
post = Post.objects.get(pk=1)
# Update — single instance
post.title = "Renamed"
post.save()
# Update — bulk via queryset
Post.objects.filter(author=alice).update(title=F("title") + " (by Alice)")
# Delete — single instance
post.delete()
# Delete — bulk
Post.objects.filter(published_at__isnull=True).delete()
Async API (same names with a prefix)
from blog.models import Author, Post
async def main():
alice = await Author.objects.acreate(name="Alice", email="a@x.com")
post = await Post.objects.acreate(title="Hi", body="...", author=alice)
async for p in Post.objects.filter(author=alice):
print(p.title)
await Post.objects.filter(pk=post.pk).aupdate(title="Hi!")
await post.adelete()
Atomic transactions
from dorm import transaction
with transaction.atomic():
alice = Author.objects.create(name="Alice", email="a@x.com")
Post.objects.create(title="t", body="b", author=alice)
# any exception here rolls back both inserts
Documentation
The full documentation, tutorials and API reference are published at:
https://rroblf01.github.io/d-orm/
You will find the getting-started guide, complete examples, the API reference and production deployment notes there.
Contributing
Everyone is welcome to get involved! If you want to suggest changes, propose improvements or discuss the direction of the project, open an issue or a pull request on this repository. Discussions, ideas and critiques are very welcome.
License
See LICENSE.
Project details
Release history Release notifications | RSS feed
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 djanorm-2.5.0.tar.gz.
File metadata
- Download URL: djanorm-2.5.0.tar.gz
- Upload date:
- Size: 856.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a20577382d789107990bd6ac073c5b2b8671c9bd6aadeec97430a4f1f07b5913
|
|
| MD5 |
ac0ca78a9daef25622024c12478f92c4
|
|
| BLAKE2b-256 |
1298722e8ee8435b07ba21cb838346bc4add81984270c86545b1cf6e98794ed8
|
Provenance
The following attestation bundles were made for djanorm-2.5.0.tar.gz:
Publisher:
publish.yml on rroblf01/d-orm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
djanorm-2.5.0.tar.gz -
Subject digest:
a20577382d789107990bd6ac073c5b2b8671c9bd6aadeec97430a4f1f07b5913 - Sigstore transparency entry: 1428802082
- Sigstore integration time:
-
Permalink:
rroblf01/d-orm@0c751ac067a71b3e1bc23750ccf70787b8e8f01b -
Branch / Tag:
refs/tags/v2.5.0 - Owner: https://github.com/rroblf01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0c751ac067a71b3e1bc23750ccf70787b8e8f01b -
Trigger Event:
release
-
Statement type:
File details
Details for the file djanorm-2.5.0-py3-none-any.whl.
File metadata
- Download URL: djanorm-2.5.0-py3-none-any.whl
- Upload date:
- Size: 278.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e680bc4a17d10aa05b99108bcc012c594c81b7557f9c0fe54a17bdfb5cba7b7
|
|
| MD5 |
34a34bc63ed39ecc72e89c797006373e
|
|
| BLAKE2b-256 |
64ba3a4f630941462a1060e7643772cac8278266330c5823bf7a468df087dd31
|
Provenance
The following attestation bundles were made for djanorm-2.5.0-py3-none-any.whl:
Publisher:
publish.yml on rroblf01/d-orm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
djanorm-2.5.0-py3-none-any.whl -
Subject digest:
8e680bc4a17d10aa05b99108bcc012c594c81b7557f9c0fe54a17bdfb5cba7b7 - Sigstore transparency entry: 1428802084
- Sigstore integration time:
-
Permalink:
rroblf01/d-orm@0c751ac067a71b3e1bc23750ccf70787b8e8f01b -
Branch / Tag:
refs/tags/v2.5.0 - Owner: https://github.com/rroblf01
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0c751ac067a71b3e1bc23750ccf70787b8e8f01b -
Trigger Event:
release
-
Statement type: