Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A utility for query-like predicate testing of Django model instances
Branch: master

try rst

latest commit aa0aa9eabc
@ptone authored
Failed to load latest commit information.
predicate bump version
tests django-extensions isn't required for running tests
.gitignore initial commit
AUTHORS initial commit
CONTRIBUTING.rst try rst
LICENSE initial commit packaging fixes
README.rst readme verbosity fix packaging fixes



django-predicate provides a Q like object to facilitate the question: "would this model instance be part of a query" but without running the query or even saving the object.


Install django-predicate:

pip install django-predicate

Then use the P object just as you would Q objects:

from predicate import P

p = P(some_field__startswith="hello", age__gt=20)

You can then call the eval method with a model instance to check whether it passes the conditions:

model_instance = MyModel(some_field="hello there", age=21)
other_model_instance = MyModel(some_field="hello there", age=10)
>>> True
>>> False

or you can use Python's in operator.

model_instance in p
>>> True

Even though a predicate is not a true container class - it can be used as (and was designed as being) a virtual "set" of objects that meets some condiiton.

Like Q objects, P objects can be &'ed and |'ed together to form more complex logic groupings.

In fact, P objects are actually a subclass of Q objects, so you can use them in queryset filter statements:

qs = MyModel.objects.filter(p)

If you have a situation where you want to use querysets and predicates based on the same conditions, it is far better to start with the predicate. Because of the way querysets assume a SQL context, it is non-trivial to reverse engineer them back into a predicate. However as seen above, it is very straightforward to create a queryset based on a predicate.

Something went wrong with that request. Please try again.