From fb4fd27dc2d85812755e0b3e643ac03053189c07 Mon Sep 17 00:00:00 2001 From: long2ice Date: Tue, 11 Feb 2020 13:09:14 +0800 Subject: [PATCH] add `between and` support (#289) --- CHANGELOG.rst | 1 + CONTRIBUTORS.rst | 1 + docs/query.rst | 1 + tests/test_filters.py | 8 ++++++++ tortoise/filters.py | 12 +++++++++++- 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 959d9207a..c5c7d65c8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,7 @@ Changelog 0.15.11 ------- - Added ``ordering`` option for model ``Meta`` class to apply default ordering +- Added ``range`` filter to support ``between and`` syntax 0.15.10 ------- diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 666c8cdaf..f715f99d7 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -23,6 +23,7 @@ Contributors * Rebecca Klauser ``@svms1`` * Sina Sohangir ``@sinaso`` * Weiming Dong ``@dongweiming`` +* Jinlong Peng ``@long2ice`` Special Thanks ============== diff --git a/docs/query.rst b/docs/query.rst index a57d3c559..b8751ecfe 100644 --- a/docs/query.rst +++ b/docs/query.rst @@ -255,6 +255,7 @@ When using ``.filter()`` method you can use number of modifiers to field names t - ``gt`` - greater than passed value - ``lte`` - lower or equals than passed value - ``lt`` - lower than passed value +- ``range`` - between and given two values - ``isnull`` - field is null - ``not_isnull`` - field is not null - ``contains`` - field contains specified substring diff --git a/tests/test_filters.py b/tests/test_filters.py index 660e180d3..eb603811a 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -220,3 +220,11 @@ async def test_gt(self): .values_list("decimal", flat=True), [Decimal("2.3"), Decimal("2.3457"), Decimal("23")], ) + + async def test_between_and(self): + self.assertEqual( + await DecimalFields.filter( + decimal__range=(Decimal("1.2344"), Decimal("1.2346")) + ).values_list("decimal", flat=True), + [Decimal("1.2345")], + ) diff --git a/tortoise/filters.py b/tortoise/filters.py index 150f11d5b..81810870a 100644 --- a/tortoise/filters.py +++ b/tortoise/filters.py @@ -1,6 +1,6 @@ import operator from functools import partial -from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional +from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional, Tuple from pypika import Table from pypika.enums import SqlTypes @@ -53,6 +53,10 @@ def not_in(field: Term, value: Any) -> Criterion: return field.notin(value) | field.isnull() +def between_and(field: Term, value: Tuple[Any, Any]) -> Criterion: + return field.between(value[0], value[1]) + + def not_equal(field: Term, value: Any) -> Criterion: return field.ne(value) | field.isnull() @@ -235,6 +239,12 @@ def get_filters_for_field( "source_field": source_field, "operator": operator.lt, }, + f"{field_name}__range": { + "field": actual_field_name, + "source_field": source_field, + "operator": between_and, + "value_encoder": list_encoder, + }, f"{field_name}__contains": { "field": actual_field_name, "source_field": source_field,