Skip to main content

SQLAlchemy to Django integration library

Project description

Build status Downloads Downloads


Base

Small package for integration SQLAlchemy into an existent Django project. The primary use case of this package is building complex queries that are not possible with the Django ORM.

You need to include aldjemy at the end of INSTALLED_APPS. When models are imported, aldjemy will read all models and contribute sa attribute to them. sa attribute is a class, mapped to Table class.

Internally, aldjemy generates tables from Django models. This is an important distinction from the standard decision of using SQLAlchemy reflection.

Code example:

User.sa.query().filter(User.sa.username=='Brubeck')

M2M sample:

User.sa.query().join(User.sa.groups).filter(Group.sa.name=="GROUP_NAME")

Explicit joins is part of SQLAlchemy philosophy, so aldjemy can’t get you exactly the same experience as Django. But aldjemy is not positioned as Django ORM drop-in replacement. It’s a helper for special situations.

We have some stuff in the aldjemy cache too:

from aldjemy import core
core.Cache.models # All generated models
core.get_tables() # All tables, and M2M tables too

You can use this stuff if you need - maybe you want to build queries with tables, or something like this.

Settings

You can add your own field types to map django types to sqlalchemy ones with ALDJEMY_DATA_TYPES settings parameter. Parameter must be a dict, key is result of field.get_internal_type(), value must be a one arg function. You can get idea from aldjemy.types.

Also it is possible to extend/override list of supported SQLALCHEMY engines using ALDJEMY_ENGINES settings parameter. Parameter should be a dict, key is substring after last dot from Django database engine setting (e.g. sqlite3 from django.db.backends.sqlite3), value is SQLAlchemy driver which will be used for connection (e.g. sqlite, sqlite+pysqlite). It could be helpful if you want to use django-postgrespool.

Mixins

Often django models have helper function and properties that helps to represent the model’s data (__unicode__), or represent some model based logic.

To integrate it with aldjemy models you can put these methods into a separate mixin:

class TaskMixin(object):
    def __unicode__(self):
        return self.code

class Task(TaskMixin, models.Model):
    aldjemy_mixin = TaskMixin
    code = models.CharField(_('code'), max_length=32, unique=True)

Voilà! You can use unicode on aldjemy classes, because this mixin will be mixed into generated aldjemy model.

If you want to expose all methods and properties without creating a separate mixin class, you can use the aldjemy.meta.AldjemyMeta metaclass:

from aldjemy.meta import AldjemyMeta

class Task(models.Model):
    code = models.CharField(_('code'), max_length=32, unique=True)

    def __unicode__(self):
        return self.code

    __metaclass__ = AldjemyMeta

The result is same as with the example above, only you didn’t need to create the mixin class at all.

Also note that with Python 3, the syntax is a bit different:

class Task(models.Model, metaclass=AldjemyMeta):
    code = models.CharField(_('code'), max_length=32, unique=True)

    def __str__(self):
        return self.code

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

aldjemy-0.9.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

aldjemy-0.9.0-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file aldjemy-0.9.0.tar.gz.

File metadata

  • Download URL: aldjemy-0.9.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for aldjemy-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4dc07ad4f30fca42529fdbc6d7350329c5ca6517d842e618146f4ca445c19a13
MD5 175ed057f7dfbd3572dd5a9c4f5b0f2a
BLAKE2b-256 b483379e1078b6ef92a9b333c9b4438e674cda228205192290cd36a98a2adec7

See more details on using hashes here.

File details

Details for the file aldjemy-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: aldjemy-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 26.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for aldjemy-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc62d868d1bac0714b91e625e0b7162534034d487f17f9569053e38053111e7b
MD5 af0bf798b3ccd74f4438410db20b9add
BLAKE2b-256 9fc65081d6aa6a063108904ba0fca5fc0ed93f9284908b589397f12b5a8e63db

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