Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

pypm install django-predicate

How to install django-predicate

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install django-predicate
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.1.1 Available View build log
Windows (64-bit)
0.1.1 Available View build log
Mac OS X (10.5+)
0.1.1 Available View build log
Linux (32-bit)
0.1.1 Available View build log
0.1 Available View build log
Linux (64-bit)
0.1.1 Available View build log
0.1 Available View build log
Lastest release
version 0.1.1 on Nov 23rd, 2012

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:

System Message: ERROR/3 (<string>, line 13)

Unknown directive type "code-block".

.. code-block:: console

    pip install django-predicate

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

System Message: ERROR/3 (<string>, line 19)

Unknown directive type "code-block".

.. code-block:: python

    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:

System Message: ERROR/3 (<string>, line 28)

Unknown directive type "code-block".

.. code-block:: python

    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.

System Message: ERROR/3 (<string>, line 39)

Unknown directive type "code-block".

.. code-block:: python

    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:

System Message: ERROR/3 (<string>, line 53)

Unknown directive type "code-block".

.. code-block:: python

    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.

Subscribe to package updates

Last updated Nov 23rd, 2012

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.