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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-expression-filter-0.0.1.tar.gz
.
File metadata
- Download URL: django-expression-filter-0.0.1.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.6 Linux/4.15.0-147-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 604105e81c3ae4d6faea6d89ff5dd150b888f736e1bc88c77c65181ba8998a08 |
|
MD5 | 96a6b6e52be263aca55accc80ae6bc52 |
|
BLAKE2b-256 | 043c4e50f494de1a37132031eb7541698bdca442f267195b72d8ac7d006b7804 |
File details
Details for the file django_expression_filter-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_expression_filter-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.6 Linux/4.15.0-147-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 225fc4deb4d16e6c258acfd75ac5d337183b415ea806c3453cc9d8548d81a87d |
|
MD5 | 1b1c2b3237952cc171492d5361bcb61b |
|
BLAKE2b-256 | 56e1f6d6ad53d5cf69a149f29f60c3acad97fd5303d98bd67e73e6ecfd060c82 |