Skip to main content

Notice

2022-04-27: This library is currently unmaintained, since I no longer use Django. If there is any interest, I’m still happy to review pull requests and consider adding maintainers. - @lucaswiman

django-predicate

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.

Quickstart

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)
p.eval(model_instance)
>>> True
p.eval(other_model_instance)
>>> 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)

P objects also support QuerySet-like filtering operations that can be applied to an arbitrary iterable: P.get(iterable), P.filter(iterable), and P.exclude(iterable):

model_instance = MyModel(some_field="hello there", age=21)
other_model_instance = MyModel(some_field="hello there", age=10)
p.filter([model_instance, other_model_instance]) == [model_instance]
>>> True
p.get([model_instance, other_model_instance]) == model_instance
>>> True
p.exclude([model_instance, other_model_instance]) == [other_model_instance]
>>> True

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.

Development

These instructions assume you have Postgres installed and all referenced Python versions installed and activated, e.g. with Pyenv.

To run the tests locally, do the following:

  1. Clone this repo.

  2. Start Postgres in the background for the Postgres tests with postgres -D ~/postgres.

  3. Activate all tested Python versions used in this repo: pyenv local 3.6 3.7 3.10.

  4. (Optional) Create a virtualenv and activate it: python -m venv .venv, source .venv/bin/activate.

  5. (Optional) Upgrade pip to prevent platform issues pip install --upgrade pip

  6. Install Tox for running tests: pip install tox.

  7. Run all tests by issuing command tox with no arguments.

Changelog

2.0.1

  • Added support for Python 3.7 and 3.10, and Django 2.2, 3.2, and 4.1 (see tox.ini for compatible Python/Django version tuples).

  • Converted test runner from Nose to Pytest.

  • BREAKING Dropped support for Python 2.7, Python 3.5, and Django 1.9.

2.0.0 (Unreleased)

This version was pushed to Master but was not pushed to PyPI.

  • Added deprecation warning to README.

  • Added Travis CI config.

  • BREAKING Dropped support for Django 1.7 and 1.8.

1.4.0

This version and below aren’t covered in this changelog.

Metadata

Release files for django-predicate 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-predicate 2.0.1
File Size Uploaded Provenance Actions
django-predicate-2.0.1.tar.gz 11.2 kB Without provenance Details for django-predicate-2.0.1.tar.gz

Release files / django-predicate-2.0.1.tar.gz

Download URL django-predicate-2.0.1.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c3ef13826151b782c55a030f17ffe62b5b56d7de4b7619f3273aac6f8ac1e024
BLAKE2b-256 checksum
How to use checksums
4e1b5a6f304d6a21256fd8fefa2918ff9c1fa527b8eeef2d273c1ac4ca6b12b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for django-predicate 2.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page