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

For more details and examples check the documentation.

Requirements

  • Python: 3.6, 3.7, 3.8, 3.9
  • Django: 2.2, 3.0, 3.1
  • Django-filter: 2.3+

Limitations

Performance

Because this app preserved Django Filtersets and filters them agains fields that are not Database fields all filtering happens in memory. Compared with direct sql optimized queries that might be slower and more memory intensive.

Limit on returned results

In theory there is no limit for most databases how many results can be returned from a filter query unless the database implements a limit which will impact how many results django-property-filter can return. Sqlite3 defines different limits depending on the version used. For further details see the documentation for limitations

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.

from django_property_filter import PropertyNumberFilter, PropertyFilterSet

class BookFilterSet(PropertyFilterSet):
    prop_number = PropertyNumberFilter(field_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

Development

Run the Django Test Project to see the filters in action

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

Issue Raising

Raising bugs, feature requests and questions is possible via the public repository django-property-filter-issue-reporting

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-1.0.0.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

django_property_filter-1.0.0-py2.py3-none-any.whl (15.8 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django-property-filter-1.0.0.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0

File hashes

Hashes for django-property-filter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3137a10aefd1aedf73cbbec198c211ad4c8a83edf4f1b3a953b2cb7501963717
MD5 48db642a97df4c108a133fc4b72315d8
BLAKE2b-256 1c9f5e9f8b96341843174adcaf1b0c576cc6f5addf4566fb950cc483766aa3e3

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: django_property_filter-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0

File hashes

Hashes for django_property_filter-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b5168e4cec57c94f01fe8569149080a5f2a7d16765110e1749a4a183f95ce746
MD5 e75d684c57266cf0820f51bbbd944ba8
BLAKE2b-256 0b4a7e4dfcb19c8ed83505f7a61f7eb3b03064ddf010ee85afc4e38810b09485

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