Skip to main content

Django model integration for MoSQL.

Project description

https://travis-ci.org/uranusjr/django-mosql.png?branch=master

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 untra 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 all you need is 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 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.

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

django-mosql-0.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

django_mosql-0.1-py27-none-any.whl (8.3 kB view details)

Uploaded Python 2.7

File details

Details for the file django-mosql-0.1.tar.gz.

File metadata

  • Download URL: django-mosql-0.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-mosql-0.1.tar.gz
Algorithm Hash digest
SHA256 d5f0a7f48bb04f70d420d2533ab029880d9ae872223095aadcffc135033a3459
MD5 34b3bd5b13cbc4ffbc360ac31bc7ffab
BLAKE2b-256 7ecbb0c0a568f08e734f6aafcb744970fd87ab39daea48ae04845d2284d5548f

See more details on using hashes here.

File details

Details for the file django_mosql-0.1-py27-none-any.whl.

File metadata

File hashes

Hashes for django_mosql-0.1-py27-none-any.whl
Algorithm Hash digest
SHA256 0b637d905bb7b6c9ffe98c69ada5d83ca2ee0475f9ca1d895b05ec7cd450c1e8
MD5 da59a2845defd9315d2c2d87685d52e6
BLAKE2b-256 6c6ba9ae5224f9b9c2345099e575b0576b2ad4d38a30606323c0abfca41a56d6

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