Skip to main content

SQLAlchemy for your Django models

Project description

Aldjemy Logo

Downloads Downloads

Aldjemy integrates SQLAlchemy into an existing Django project, to help you build complex queries that are difficult for the Django ORM.

While other libraries use SQLAlchemy reflection to generate SQLAlchemy models, Aldjemy generates the SQLAlchemy models by introspecting the Django models. This allows you to better control what properties in a table are being accessed.

Installation

Add aldjemy to your INSTALLED_APPS. Aldjemy will automatically add an sa attribute to all models, which is an SQLAlchemy Model.

Example:

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

Explicit joins are part of the SQLAlchemy philosophy, so don’t expect Aldjemy to be a Django ORM drop-in replacement. Instead, you should use Aldjemy to help with special situations.

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.table.

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 (__str__), or represent some model based logic.

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

class TaskMixin:
    def __str__(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 __str__ 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:

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

    def __str__(self):
        return self.code

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

Release Process

  1. Make a Pull Request with updated changelog and bumped version of the project

    poetry version (major|minor|patch) # choose which version to bump
  2. Once the pull request is merged, create a github release with the same version, on the web console or with github cli.

    gh release create
  3. Enjoy!

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-2.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

aldjemy-2.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aldjemy-2.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.6 Linux/5.8.0-1039-azure

File hashes

Hashes for aldjemy-2.2.tar.gz
Algorithm Hash digest
SHA256 6c8215164a983117b4a3c28a43cec7ab7b564c938b6fe4797a970edd8a35cc9d
MD5 138ddec8be0ee3accd7683e70723839a
BLAKE2b-256 12914e94a41d4693108cc2ce33469d04b889a2c0ae051e134edc3575d364036a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aldjemy-2.2-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.6 Linux/5.8.0-1039-azure

File hashes

Hashes for aldjemy-2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 86c8c844efe22a54151397bd57c0abc34387735835df15c86819241bdd5030ba
MD5 968c700ca9c3876cca564d4ac383f53b
BLAKE2b-256 cb36e9291bfc54e6cb6ed35f8cb6a6530357790c10beaae61b009404d28bc157

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