-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Make default filter, sort and split_str a no-op, plus various improvements #272
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
*.sqlite-journal | ||
.DS_Store | ||
.coverage | ||
.pypirc | ||
coverage.xml | ||
dist/ | ||
fastapi_filter.sqlite | ||
poetry.toml | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# Unit test / coverage reports | ||
test-results/ | ||
junit.xml | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,23 +14,22 @@ class BaseFilterModel(BaseModel, extra=Extra.forbid): | |
|
||
Provides the interface for filtering and ordering. | ||
|
||
## Ordering | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious why |
||
|
||
# Ordering | ||
|
||
## Query string examples: | ||
### Query string examples:: | ||
|
||
>>> "?order_by=-created_at" | ||
>>> "?order_by=created_at,updated_at" | ||
>>> "?order_by=+created_at,-name" | ||
|
||
## Limitation | ||
### Limitation | ||
|
||
Sorting doesn't support related fields, you can only use the attributes of the targeted model/collection. | ||
For example, you can't use `related_model__attribute`. | ||
|
||
# Filtering | ||
## Filtering | ||
|
||
## Query string examples: | ||
### Query string examples:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have never been a docstring formatting expert, why do you use the |
||
|
||
>>> "?my_field__gt=12&my_other_field=Tomato" | ||
>>> "?my_field__in=12,13,15&my_other_field__not_in=Tomato,Pepper" | ||
|
@@ -44,7 +43,7 @@ class Constants: # pragma: no cover | |
prefix: str | ||
|
||
def filter(self, query): # pragma: no cover | ||
... | ||
return query | ||
|
||
@property | ||
def filtering_fields(self): | ||
|
@@ -53,7 +52,7 @@ def filtering_fields(self): | |
return fields.items() | ||
|
||
def sort(self, query): # pragma: no cover | ||
... | ||
return query | ||
|
||
@property | ||
def ordering_values(self): | ||
|
@@ -68,7 +67,7 @@ def ordering_values(self): | |
|
||
@validator("*", pre=True) | ||
def split_str(cls, value, field): # pragma: no cover | ||
... | ||
return value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I used |
||
|
||
@validator("*", pre=True, allow_reuse=True, check_fields=False) | ||
def strip_order_by_values(cls, value, values, field): | ||
|
@@ -126,10 +125,9 @@ def validate_order_by(cls, value, values, field): | |
def with_prefix(prefix: str, Filter: Type[BaseFilterModel]): | ||
"""Allow re-using existing filter under a prefix. | ||
|
||
Example: | ||
```python | ||
from pydantic import BaseModel | ||
Example:: | ||
|
||
from pydantic import BaseModel | ||
from fastapi_filter.filter import FilterDepends | ||
|
||
class NumberFilter(BaseModel): | ||
|
@@ -138,28 +136,26 @@ class NumberFilter(BaseModel): | |
class MainFilter(BaseModel): | ||
name: str | ||
number_filter: Optional[Filter] = FilterDepends(with_prefix("number_filter", Filter)) | ||
``` | ||
|
||
As a result, you'll get the following filters: | ||
* name | ||
* number_filter__count | ||
|
||
# Limitation | ||
## Limitation | ||
|
||
The alias generator is the last to be picked in order of prevalence. So if one of the fields has a `Query` as | ||
default and declares an alias already, this will be picked first and you won't get the prefix. | ||
|
||
Example: | ||
```python | ||
from pydantic import BaseModel | ||
Example:: | ||
|
||
from pydantic import BaseModel | ||
|
||
class NumberFilter(BaseModel): | ||
count: Optional[int] = Query(default=10, alias=counter) | ||
|
||
class MainFilter(BaseModel): | ||
name: str | ||
number_filter: Optional[Filter] = FilterDepends(with_prefix("number_filter", Filter)) | ||
``` | ||
|
||
As a result, you'll get the following filters: | ||
* name | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the poetry upgrade to a separate PR?