Skip to content

Commit

Permalink
add between and support (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice authored Feb 11, 2020
1 parent f277a70 commit fb4fd27
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Contributors
* Rebecca Klauser ``@svms1``
* Sina Sohangir ``@sinaso``
* Weiming Dong ``@dongweiming``
* Jinlong Peng ``@long2ice``

Special Thanks
==============
Expand Down
1 change: 1 addition & 0 deletions docs/query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
)
12 changes: 11 additions & 1 deletion tortoise/filters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fb4fd27

Please sign in to comment.