Skip to main content

SQLAlchemy tables disguised as Django models.

Project description

Polyjuice ⚗️🧙‍♂️

SQLAlchemy tables disguised as Django models.

Build Status License

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. 🎉🍻

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

polyjuice-0.1.0.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

polyjuice-0.1.0-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page