Package: django-property-filter
Django Property FilterDjango-property-filter is an extension todjango-filterand 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.LicenseVersionGithub ActionCoverageWheelImplementationStatusDownloadsSupported versionsFor more details and examples check thedocumentation.RequirementsPython: 3.7, 3.8, 3.9, 3.10, 3.11, 3.12Django: 3.2, 4.0, 4.1, 5.0Django-filter: 23.5+LimitationsPerformanceBecause 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 resultsIn 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 forlimitationsInstallationInstall using pip:pipinstalldjango-property-filterThen add 'django_property_filter' to your INSTALLED_APPS.INSTALLED_APPS=[...'django_property_filter',]UsageExample ModelOur Modelfromdjango.dbimportmodelsclassBookSeries(models.Model):name=models.CharField(max_length=255)@propertydefbook_count(self):returnBook.objects.filter(series=self).count()classBook(models.Model):title=models.CharField(max_length=255)price=models.DecimalField()discount_percentage=models.IntegerField()author=models.TextField()series=models.ForeignKey(BookSeries)@propertydefdiscounted_price(self):returnself.price*self.discount_percentage\100Implicit Filter CreationIf 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.::fromdjango_property_filterimportPropertyFilterSet,PropertyNumberFilterclassBookFilterSet(PropertyFilterSet):classMeta:model=Bookexclude=['price']property_fields=[('discounted_price',PropertyNumberFilter,['lt','exact']),('series.book_count.',PropertyNumberFilter,['gt','exact']),]This will create 4 Filters1.) 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".The order in which the property filters are applied are in the same order as
they are defined. In the above filter will be in order of1.) discounted_price: lt
2.) discounted_price: exact
3.) series.book_count: gt
4.) series.book_count: exactThis can be usefull to speed up the filtering by property filters if filters
are defined first that are likely to filter a large number of values.
E.g. a boolean field might be filtering an average of half the values, while a
field representing a name will likely filter fewer.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 CreationIt is also possible to create Filters explicitely.fromdjango_property_filterimportPropertyNumberFilter,PropertyFilterSetclassBookFilterSet(PropertyFilterSet):prop_number_gte=PropertyNumberFilter(field_name='discounted_price',lookup_expr='gte')prop_number_lt=PropertyNumberFilter(field_name='discounted_price',lookup_expr='lt')classMeta:model=NumberClassfields=['prop_number_gte','prop_number_lt']This creates a "greater than or equel" and a "less than" filter for the
discounted_price property.The order in which the property filters are applied are in the same order as
they are defined. In the above filter will be in order of1.) prop_number_gte
2.) prop_number_ltThis can be usefull to speed up the filtering by property filters if filters
are defined first that are likely to filter a large number of values.
E.g. a boolean field might be filtering an average of half the values, while a
field representing a name will likely filter fewer.DevelopmentRun the Django Test Project to see the filters in actiongo to "tests\django_test_proj"run "python manage.py makemigrations"run "python manage.py migrate"run "python manage.py setup_data"run "python manage.py runserver"Issue RaisingRaising bugs, feature requests and questions is possible via the public repositorydjango-property-filter-issue-reporting