Skip to main content

A class for Django filters to allow direct filtering on expressions, as you would normally do with annotations, so the annotation does not need to happen.

Project description

About

(WIP)

This package provides a class that enables direct filtering on an expression that you’d usually use as an annotation.

Usage

The class AnnotationBypass takes 3 arguments. The first is any valid annotation value, the second is the lookup string you’d use in the filter kwarg name, the third is the value you’d use for the filter.

from expression_filter import AnnotationBypass

filter = AnnotationBypass(Sum('book__page_count'), 'gte', 1000)

But why

Consider the following:

Book.objects.annotate(words_per_page=F('word_count') / F('page_count')).filter(words_per_page__gte=100)

This executes the following query:

SELECT `books_book`.`id`,
       `books_book`.`word_count`,
       `books_book`.`page_count`,
       `books_book`.`word_count` / `books_book`.`page_count` AS `words_per_page`
FROM `books_book`
WHERE `books_book`.`word_count` / `books_book`.`page_count` >= 100

Notice that the expression is duplicated, as using the words_per_page annotation by name in the WHERE clause is not allowed. Usually the annotated value is not needed in the response, and depending on how complex the annotation is or how many objects are returned, this could have a performance impact.

This is better:

from expression_filter import AnnotationBypass

Book.objects.filter(AnnotationBypass(F('word_count') / F('page_count'), 'gte', 100))
SELECT `books_book`.`id`,
       `books_book`.`word_count`,
       `books_book`.`page_count`
FROM `books_book`
WHERE `books_book`.`word_count` / `books_book`.`page_count` >= 100

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-expression-filter-0.0.1.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

django_expression_filter-0.0.1-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

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