Blog

Waldemar Kornewald on May 19, 2010

djangoappengine updates: IN and != queries and DecimalField

Django-nonrel's App Engine backend (djangoappengine) now finally supports all query features available on App Engine. Additionally, we now support DecimalField, thanks to Mariusz Kryński!

Let's start with "!=" query support. You can now create queries like this:

Post.objects.exclude(category='sql')

This has the same behavior as the App Engine code Post.all().filter('category !=', 'sql'). Internally, this actually gets converted into two queries (even with App Engine's native API): "category > 'sql'" and "category < 'sql'".

We also now support IN queries like this one:

Post.objects.filter(category__in=['django', 'NoSQL'])

This has the same behavior as the App Engine code Post.all().filter('category IN', ['django', 'fun']). Again, internally this gets converted into two queries: "category = 'django'" and "category = 'NoSQL'".

Note that you can't create queries which would need more than 30 sub-queries.

Finally, you can now use DecimalField:

from decimal import Decimal
from django.db import models

class DecimalModel(models.Model):
    decimal = models.DecimalField(max_digits=9, decimal_places=2)

result = DecimalModel.objects.get(decimal=Decimal('45.60'))

The difference between decimals and floats is that decimals are exact whereas a floats have rounding errors. For example, if you enter "2.3" this into a Python console you get:

>>> 2.3
2.2999999999999998

In contrast, a decimal would stay exactly "2.3".

So, what's next? During this Google I/O we might get to know about upcoming App Engine query features. In particular, you just have to watch the Next gen queries session. It'll discuss exciting features like more complex queries with OR and NOT operations and a solution to the exploding indexes problem. Of course, the App Engine backend will be extended to support all the new features once they are officially available.