SQLAlchemy for your Django models
Project description
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
Make a Pull Request with updated changelog and bumped version of the project
hatch version (major|minor|patch) # choose which version to bump
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
Enjoy!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aldjemy-3.1.tar.gz
.
File metadata
- Download URL: aldjemy-3.1.tar.gz
- Upload date:
- Size: 35.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 218ece3e81dbd7eda9e37fd87d084b4124c215aef0a6b0bc41b81fcff9afea39 |
|
MD5 | a4abe0837863042a2f2ddf8c6608fc2c |
|
BLAKE2b-256 | 7efb749af2807d9fff51c65d5c164eb7c64388d72b0285ea8f6cda5ea3add14e |
Provenance
The following attestation bundles were made for aldjemy-3.1.tar.gz
:
Publisher:
publish.yml
on aldjemy/aldjemy
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
aldjemy-3.1.tar.gz
- Subject digest:
218ece3e81dbd7eda9e37fd87d084b4124c215aef0a6b0bc41b81fcff9afea39
- Sigstore transparency entry: 149134986
- Sigstore integration time:
- Predicate type:
File details
Details for the file aldjemy-3.1-py3-none-any.whl
.
File metadata
- Download URL: aldjemy-3.1-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f04221027be4116cf89335b52a13defcfbd2cf6190b40a42dcc34a0ad218f16 |
|
MD5 | 7d2f04f5aabd7ed7ecc491a1b2139c09 |
|
BLAKE2b-256 | 32df47631d491f65cd45fafbc0863eae0331e41c7817e637028a2873f46b080c |
Provenance
The following attestation bundles were made for aldjemy-3.1-py3-none-any.whl
:
Publisher:
publish.yml
on aldjemy/aldjemy
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
aldjemy-3.1-py3-none-any.whl
- Subject digest:
1f04221027be4116cf89335b52a13defcfbd2cf6190b40a42dcc34a0ad218f16
- Sigstore transparency entry: 149134987
- Sigstore integration time:
- Predicate type: