An asynchronous ORM inspired by Django's ORM, built on top of SQLAlchemy.
Project description
AsyncDjangoORM
AsyncDjangoORM is an asynchronous ORM inspired by Django's ORM, built on top of SQLAlchemy. It provides Django-like Querysets and AsyncManagers, allowing you to interact with databases using Python async/await.
Ideal for telegram bots while building application with aiogram
Features
Full async support using async/await.
Django-style Queryset and AsyncManager.
CRUD operations: get, create, get_or_create, update_or_create.
Query methods: filter, exclude, order_by, annotate, aggregate, bulk_create, bulk_update, bulk_delete.
Relation handling: select_related and prefetch_related.
Supports PostgreSQL, MySQL, and SQLite.
Lightweight, flexible, and easy to integrate.
Installation
Install via pip:
pip install asyncdjangoorm
# PostgreSQL
pip install asyncdjangoorm[postgres]
# MySQL
pip install asyncdjangoorm[mysql]
# SQLite (default)
pip install asyncdjangoorm[sqlite]
Database Configuration
SQLite (default)
export DATABASE_URL="sqlite+aiosqlite:///./mydb.db"
PostgreSQL (asyncpg)
export DATABASE_URL="postgresql+asyncpg://user:password@localhost:5432/mydb"
MySQL (aiomysql)
export DATABASE_URL="mysql+aiomysql://user:password@localhost:3306/mydb"
Getting Started with asyncdjangoorm
asyncdjangoorm is an asynchronous ORM inspired by Django, built on top of SQLAlchemy. It integrates easily with aiogram for Telegram bots.
1️⃣ Install the package
pip install asyncdjangoorm
2️⃣ Define Models
You can define your models using SQLAlchemy and attach AsyncManager for async operations.
# models.py
from sqlalchemy import Column, Integer, String
from asyncdjangoorm import TimeStampedModel, AsyncManager
class MyModel(TimeStampedModel):
__tablename__ = "my_model"
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
value = Column(Integer)
# Attach AsyncManager
MyModel.objects = AsyncManager(MyModel)
TimeStampedModelautomatically addscreated_atandupdated_atfields.
3️⃣ Initialize the Database
Before using your models, initialize all tables:
# init_db_example.py
import asyncio
from asyncdjangoorm import init_db
from models import MyModel
async def main():
await init_db() # Create all tables
if __name__ == "__main__":
asyncio.run(main())
4️⃣ Using the ORM
Basic usage examples:
# usage_example.py
import asyncio
from models import MyModel
async def main():
# Create a new object
await MyModel.objects.create(name="Test", value=42)
# Fetch all objects
items = await MyModel.objects.all()
print(items)
# Filter objects
filtered = await MyModel.objects.filter(value__gt=10)
print(filtered)
# Get or create an object
obj, created = await MyModel.objects.get_or_create(name="Example")
print(obj, "Created:", created)
if __name__ == "__main__":
asyncio.run(main())
5️⃣ Integrating with aiogram
Example Telegram bot using asyncdjangoorm:
# bot_example.py
import asyncio
from aiogram import Bot, Dispatcher, types
from asyncdjangoorm import init_db
from models import MyModel
BOT_TOKEN = "YOUR_BOT_TOKEN"
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
async def on_startup():
await init_db() # Initialize tables
@dp.message_handler(commands=["create"])
async def create_item(message: types.Message):
await MyModel.objects.create(name="FromBot", value=99)
await message.answer("Item created!")
@dp.message_handler(commands=["list"])
async def list_items(message: types.Message):
items = await MyModel.objects.all()
text = "\n".join(f"{item.id}: {item.name} = {item.value}" for item in items) or "No items found."
await message.answer(text)
if __name__ == "__main__":
asyncio.run(on_startup())
dp.run_polling(bot)
6️⃣ Notes
-
AsyncManagerallows async CRUD operations, filtering, ordering, and annotations. -
Compatible with all Python 3 versions and aiogram 2.x and 3.x.
-
You can attach
AsyncManagerto any SQLAlchemy model.
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
File details
Details for the file asyncdjangoorm-0.1.4.tar.gz.
File metadata
- Download URL: asyncdjangoorm-0.1.4.tar.gz
- Upload date:
- Size: 243.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f89318ddeba531858eadd5b3626b041ab8aac6b6010837536560dd06d7f45dd8
|
|
| MD5 |
3237456721e9c430820c15e3d5c83ffa
|
|
| BLAKE2b-256 |
1d138c5a513193416b5197f4e64ef0b82d772e268c812ff80613c693e4833294
|