Entries tagged "django"

Stacking Django Q lookups in OR/AND fashion

Oct 1, 2008 12:49:00 AM

Just a quick one so I won't forget, I was wondering how to compile a OR lookup stack from a list of Q objects.

>>> # Import the operator built-in module
>>> import operator
>>> from django.db.models import Q
>>> from mytest.models import Test
>>> # Create an arbitrary set of test-data
>>> for x in xrange(0, 99): Test(test=x).save()
>>> # The lookups
>>> lookups = [Q(test=1), Q(test=2), Q(test=23), Q(test=42)]
>>> # Here we use reduce()
>>> print Test.objects.filter(reduce(operator.or_, lookups))
[<Test test=1>, <Test test=2>, <Test test=23>, <Test test=42>]

This also works for operator.and_.

 

Django 1.0 released

Sep 4, 2008 10:46:00 AM

It has been a bumpy ride, and it has taken some time to get there, but the Django 1.0 version has been finally released!

Read on..., 1 comment.

 

Visitors

Locations of visitors to this page