Skip to main content

Django Filter extension to support filtering by Properties

Project description

Django Property Filter

Django-property-filter is an extension to django-filter and provides functionality to filter querysets by class properties.

It does so by providing sub-classes for Filters and Filtersets to keep existing django-filter functionality.

License Version
Travis CI Coverage
Wheel Implementation
Status Downloads
Supported versions

Requirements

  • Python 3.8
  • Django-filter 2.20

Installation

Install using pip:

pip install django-property-filter

Then add 'django_property_filter' to your INSTALLED_APPS.

INSTALLED_APPS = [
    ...
    'django_property_filter',
]

Usage

Example Model

Our Model

from django.db import models

class BookSeries(models.Model):
    name = models.CharField(max_length=255)

    @property
    def book_count(self):
      return Book.objects.filter(series=self).count()

class Book(models.Model):
    title = models.CharField(max_length=255)
    price = models.DecimalField()
    discount_percentage = models.IntegerField()
    author = models.TextField()
    series = models.ForeignKey(BookSeries)

    @property
    def discounted_price(self):
      return self.price * self.discount_percentage \ 100

Implicit Filter Creation

If we want to filter by discounted price as well as number of books in a series, which both are properties and not fields in the database, we would do the following.::

from django_property_filter import (
  PropertyFilterSet,
  PropertyNumberFilter
)

class BookFilterSet(PropertyFilterSet):

  class Meta:
      model = Book
      exclude = ['price']
      property_fields = [
        ('discounted_price', PropertyNumberFilter, ['lt', 'exact']),
        ('series.book_count.', PropertyNumberFilter, ['gt', 'exact']),
      ]

This will create 4 Filters 1.) A "less than" and an "exact" filter for the "discounted_price" property of the Book Model 2.) A "greater than" and an "exact" filter for the "book_count" property of the related Model "series".

Since PropertyFilterSet is and extension to django-filter's Filterset which requires either the Meta attribute "fields" or "exclude" to be set we excluded the "price" field. If we had instead used:: fields = ['price']

It would also have created an "exact" filter for the book price.

The only difference to using a normal FilterSet from django-filter is the "property_fields" field.

The "property_fields" is a list of tuples with 3 values. 1.) The property name. If the property is on a related Model it should be separated by ".", and can span multiple levels e.g. fk.fk.fk.property 2.) The specific Property Filter to use. This is necessary since it can't be determined what the return type of the property will be in all cases 3.) The list of lookup expressions.

Explicit Filter Creation

It is also possible to create Filters explicitely. To do this we can either use FilterSet or PropertyFilterSet.

Using Filterset::

from django_filters import FilterSet
from django_property_filter import PropertyNumberFilter

class BookFilterSet(FilterSet):
    prop_number = PropertyNumberFilter(property_fld_name='discounted_price', lookup_expr='gte')

    class Meta:
        model = NumberClass
        fields = ['prop_number']

This creates a "greater than or equel" filter for the discounted_price property

The same can be achieved using a PropertyFilterSet::

from django_property_filter import PropertyNumberFilter, PropertyFilterSet

class BookFilterSet(PropertyFilterSet):
    prop_number = PropertyNumberFilter(property_fld_name='discounted_price', lookup_expr='gte')

    class Meta:
        model = NumberClass
        fields = ['prop_number']

Development

Run the Django Test Project to see the filters in action

  • go to "tests\django_test_proj"
  • run "python manage.py runserver"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-property-filter-0.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

django_property_filter-0.1-py2.py3-none-any.whl (9.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-property-filter-0.1.tar.gz.

File metadata

  • Download URL: django-property-filter-0.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for django-property-filter-0.1.tar.gz
Algorithm Hash digest
SHA256 6c24cc446bb03ff940c44784a38ad35e977420abc3ee4e6aaf2c7d474d16598d
MD5 ef4e78174db3e59f8e9dd8da17e765e4
BLAKE2b-256 0dbb7908d84c6b81b9de557f610d9488756b4d253f5fc64b63f2e1fff2310209

See more details on using hashes here.

Provenance

File details

Details for the file django_property_filter-0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_property_filter-0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for django_property_filter-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 104b5683f52d2163284764fbcbb19bd9f454a8ea59af8ce8c59cb561bea27582
MD5 e1fd4676248974f7f8008c288bbaff4b
BLAKE2b-256 a142ddab24df2b0ff765521b7fecb964f044d3ab95612dd73761cc7b47b2be5e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page