Django MoSQL
Do what Django’s ORM can’t, without the danger of raw.
Why?
Short version
Because I want to use raw SQL, but am too lazy to worry about security issues.
Long version
Django’s ORM is cool. And powerful. But ORMs are destined to be leaky, and can’t do everything you wish to. That’s why Django provides raw and extra so that you can roll your own SQL commands if you need to. But with great power comes great responsibility. You lose all the SQL security measures Django provides when you use those methods, and it can be a serious problem unless you are ultra careful.
Enters MoSQL. MoSQL, in a nutshell, is a tool that generates SQL commands automatically from Python function calls. And it takes care of the injection prevention for you. A perfect match with Django’s raw!
All this project does is introduce a Django model manager subclass, and provide an interface to use MoSQL’s function calls instead of writing SQL strings yourself. With some syntax sugar, of course.
How do I use this thing?
(Examples in this section are inspired by this blog post by Baron Schwartz.)
Let’s say you have the following model:
class Fruit(models.Model):
kind = models.CharField(max_length=10)
variety = models.CharField(max_length=10)
price = models.FloatField()
Then you only need to provide a MoManager as one of its model managers. Add djangomosql to your INSTALLED_APPS, and modify the code like this:
from djangomosql.models import MoManager
class Fruit(models.Model):
kind = models.CharField(max_length=10)
variety = models.CharField(max_length=10)
price = models.FloatField()
objects = MoManager()
And you’ll be able to generate queries like this:
from djangomosql.functions import Min
Fruit.objects.select((Min('price'), 'minprice')).group_by('kind').order_by('-kind')
Which is roughly equivalent to
SELECT fruit.*, MIN(price) as minprice FROM fruit GROUP BY kind ORDER BY kind DESC
Of course, this won’t be of much use if we can only do things Django’s ORM can. With Django MoSQL, you can achieve many funky things, like:
m = Fruit.objects
inner = m.select((Min('price'), 'minprice')).group_by('kind')
p = m.select().as_('f').order_by('f.kind').join(
inner, 'x', on={'f.kind': 'x.kind', 'f.price': 'x.minprice'}
)
Which can be translated into (again, roughly):
SELECT f.* FROM fruit AS f INNER JOIN (
SELECT *, MIN(price) as minprice FROM fruit GROUP BY kind
) AS x ON f.kind = x.kind AND f.price = x.minprice
And best of all, you get all the escaping and ORM mapping for free!
LICENSE
BSD 3-cluse license. See the content of file LICENSE.
Developing
To run tests, run python manage.py test inside the test project. You will need django-nose as well as the dependencies.
Metadata
Release files for django-mosql 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-mosql-0.5.1.tar.gz | 10.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_mosql-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.9 kB
Release files / django-mosql-0.5.1.tar.gz
| Download URL | django-mosql-0.5.1.tar.gz |
|---|---|
| Size | 10.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bbc17276a46ce281b155bf65474b2476f53746e59426b0fb2ecddf8b5b84d58b
|
|
BLAKE2b-256 checksum How to use checksums |
4e588821f071c94e934e9b58303210f0f4f03251f7207d24eb1e00a9803bee6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_mosql-0.5.1-py3-none-any.whl
| Download URL | django_mosql-0.5.1-py3-none-any.whl |
|---|---|
| Size | 13.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e1b292268312350641a77c06ea63205e6856e2f07a7b17bbd4e0ade518dcf73c
|
|
BLAKE2b-256 checksum How to use checksums |
a0194c69f85a714ad62460988a0d26d889527ba9f12fb4c8223298580cdb1220
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-mosql 0.5.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.