Polyjuice ⚗️🧙♂️
SQLAlchemy tables disguised as Django models.
Usage
Polyjuice allows you to define your database tables with SQLAlchemy Core and use them as legit Django models.
You could find Polyjuice relevant in situations where you want manage your table without the Django constraints, but still take advantage of all the goodness of Django integration and tooling when needed.
I haven't tried every use case yet, but I imagine it could suits many:
- Use other database management tools (ex: migrations with alembic)
- Take advantage of the async world (ex: build RabbitMQ consummers with aio-pika)
- Build complex SQL queries with the SQLAlchemy API and execute them through the Django database connection
- Transition to another web framework
- ¯\(ツ)/¯
Example
In an python package called my_tables.py
"""Here, define your table schemas with the SQLAlchemy core API."""
import polyjuice
from sqlalchemy import Column, ForeignKey, Integer, MetaData, String, Table
metadata = MetaData()
ProfessorTable = Table(
"hogwarts__professor",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(30), nullable=False)
)
PotionTable = Table(
'hogwarts__potion',
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(100), nullable=False),
Column(
'made_by',
Integer,
ForeignKey(ProfessorTable.c.id),
django_on_delete="CASCADE",
django_related_name="personal_potions"
)
)
In your Django project
from my_tables import PotionTable, ProfessorTable
import polyjuice
@polyjuice.mimic(ProfessorTable)
class Professor:
"""The Polyjuice decorator will turn this class into a legit Django model."""
def welcome(self):
print(f"Welcome to my class, I am Pr. {self.name} 🧙♂️")
@polyjuice.mimic(PotionTable)
class Potion:
"""This class too"""
def boil(self):
print(f"*The {self.name} potion is blurping* ⚗️")
# And you are ready to go !
severus_snape = Professor.objects.create(name="Severus Snape")
veritaserum = Professor.objects.create(name="Veritaserum", made_by=severus_snape)
assert severus_snape.personal_potions.count() == 1
Requirements
Polyjuice is currently built on top of SQLAlchemy 1.3 and Django 2.2, and requires Python 3.6.
License
Polyjuice is released into the Public Domain. 🎉🍻
Metadata
Release files for polyjuice 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| polyjuice-0.1.0.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| polyjuice-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.4 kB
Release files / polyjuice-0.1.0.tar.gz
| Download URL | polyjuice-0.1.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
55249b4c0d749c359c008304d39d237cf123292298d79f677579bbaef74530ee
|
|
BLAKE2b-256 checksum How to use checksums |
4254e1d5e0cd58d11c4d4a2b697d4689e0652731b16803e3760c5fdfb23fc795
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.9 CPython/3.7.4 Windows/10
|
Release files / polyjuice-0.1.0-py3-none-any.whl
| Download URL | polyjuice-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bfc2309720ee8c2fcaaf9f0ac4af310f34c8ba29c7c082c63a270468f4f01fda
|
|
BLAKE2b-256 checksum How to use checksums |
be6b75092b74d04a9d652943227b94ecbce87147a07735dd30dc607fcf87d1b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.9 CPython/3.7.4 Windows/10
|