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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file polyjuice-0.1.0.tar.gz.

File metadata

  • Download URL: polyjuice-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.7.4 Windows/10

File hashes

Hashes for polyjuice-0.1.0.tar.gz
Algorithm Hash digest
SHA256 55249b4c0d749c359c008304d39d237cf123292298d79f677579bbaef74530ee
MD5 326c982b6b383a0a50c0ac80f165d4a2
BLAKE2b-256 4254e1d5e0cd58d11c4d4a2b697d4689e0652731b16803e3760c5fdfb23fc795

See more details on using hashes here.

File details

Details for the file polyjuice-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: polyjuice-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.7.4 Windows/10

File hashes

Hashes for polyjuice-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfc2309720ee8c2fcaaf9f0ac4af310f34c8ba29c7c082c63a270468f4f01fda
MD5 550bf75e25a921d9292b0f74ec7e6b15
BLAKE2b-256 be6b75092b74d04a9d652943227b94ecbce87147a07735dd30dc607fcf87d1b3

See more details on using hashes here.

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