Skip to main content
https://badge.fury.io/py/django_sieve.png https://travis-ci.org/alixedi/django_sieve.png?branch=master https://pypip.in/d/django_sieve/badge.png https://coveralls.io/repos/alixedi/django_sieve/badge.png?branch=master

For a lot of web applications - Blogs come to mind as a canonical example, the queryset needs to be filtered in the views based upon some business rules. For instance, in a Blog, a “Dashboard” view - allowing editing of posts, should only ever have posts written by the user who is currently logged-in.

In general, there is a need to filter data based on some business logic - universally! i.e. across each and every view. The ugly way of doing this would be to write queries substantiating the business logic in every single view. Sieve does the same job beautifully, minimally and correctly.

Installation

We are at the cheeseshop:

pip install django_sieve

Usage

To use django_sieve in a project:

  1. Include it in INSTALLED_APPS in your settings file.

  2. Define a sieve model. A sieve model is the control for defining the filtering criteria of user data. For instance, in our example, we have a bookstore in which a user can subscribe to all the books from a particular author or all the books from a few publishers. In this case, his book list need to only contain books from the author and publishers that he has chosen. In order to do this, we define a sieve model like so:

    class Sieve(models.Model):
            group = models.ForeignKey('auth.Group')
            publisher = models.ManyToManyField(Publisher)
            author = models.ForeignKey(Author)
  3. Declare your project-wide sieve model in settings.py. For instance, if the sieve model is called Sieve and resides in the bookstore app:

    SIEVE_MODEL = 'bookstore.Sieve'
  4. Use SieveManager as the ModelManager for all the models in your project that you want to filter based on the criteria defined in the sieve model:

    class Author(models.Model):
            first_name = models.CharField(max_length=30)
            last_name = models.CharField(max_length=40)
            email = models.EmailField()
            objects = SieveManager()
  5. Use the sieve method to define your queryset in your views like so:

    class BookView(ListView):
            queryset = Book.objects.sieve(user=request.user)

That is all. Site-wide filtering of user data based on predefined criteria without having to write queries for all the views.

Please be wary of performance issues though - in our experience, django-sieve works for 90% cases especially when you need to bring everything up for a quick demo. However, as your site matures and the number of users increase, you may want to invest in profiling your views and hand-crafting these queries when and where required.

History

i.0.0 (2014)

  • Got to 93% test coverage.

0.3.0 (2014)

  • Changed the mapping from User to Group, dealt with self-referencing models etc.

0.2.0 (2014)

  • Added support for ManyToMany fields in SIEVE_MODEL

0.1.0 (2014)

  • First release on PyPI.

Metadata

Release files for django_sieve 1.0.0

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_sieve 1.0.0
File Size Uploaded Provenance
django_sieve-1.0.0.tar.gz 13.1 kB Without provenance Details

Release files / django_sieve-1.0.0.tar.gz

Download URL django_sieve-1.0.0.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7fc689c09705288ddae196a324dd535cd6ef5fd0027a266ff89e0289775a90fa
BLAKE2b-256 checksum
How to use checksums
01402b2fd76284af5ce9edf9fe4dc1bdc08aea7cd07e478fc64548b5edf9c769
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django_sieve 1.0.0

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.

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