You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/guides/input/filtering.md
+59-31Lines changed: 59 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,30 +73,52 @@ class BookFilterSchema(FilterSchema):
73
73
```
74
74
The `name` field will be converted into `Q(name=...)` expression.
75
75
76
-
When your database lookups are more complicated than that, you can explicitly specify them in the field definition using a `"q"` kwarg:
77
-
```python hl_lines="14"
78
-
from ninja import FilterSchema
76
+
When your database lookups are more complicated than that, you can annotate your fields with an instance of `FilterLookup` where you specify how you wish your field to be looked up for filtering:
By default, field-level expressions are combined using `"OR"` connector, so with the above setup, a query parameter `?search=foobar` will search for books that have "foobar" in either of their name, author or publisher.
96
+
97
+
And to make generic fields, you can make the field name implicit by skipping it:
By default, field-level expressions are combined using `"OR"` connector, so with the above setup, a query parameter `?search=foobar` will search for books that have "foobar" in either of their name, author or publisher.
104
+
105
+
??? note "Deprecated syntax"
106
+
107
+
In previous versions, database lookups were specified using `Field(q=...)` syntax:
This approach is still supported, but it is considered **deprecated** and **not recommended** for new code because:
116
+
117
+
- Poor IDE support (IDEs don't recognize custom `Field` arguments)
118
+
- Uses deprecated Pydantic features (`**extra`)
119
+
- Less type-safe and harder to maintain
120
+
121
+
The new `FilterLookup` annotation provides better developer experience with full IDE support and type safety. Prefer using `FilterLookup` for new projects.
An expression connector can take the values of `"OR"`, `"AND"` and `"XOR"`, but the latter is only [supported](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#xor) in Django starting with 4.1.
@@ -144,20 +173,19 @@ You can make the `FilterSchema` treat `None` as a valid value that should be fil
144
173
This can be done on a field level with a `ignore_none` kwarg:
0 commit comments