Skip to content

Commit 56d9146

Browse files
authored
Fixes #20499: Documented ObjectListView quick search feature for plugins (#20500)
1 parent e192f64 commit 56d9146

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

docs/plugins/development/filtersets.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ class MyModelViewSet(...):
5555
filterset_class = filtersets.MyModelFilterSet
5656
```
5757

58+
### Implementing Quick Search
59+
60+
The `ObjectListView` has a field called Quick Search. For Quick Search to work the corresponding FilterSet has to override the `search` method that is implemented in `NetBoxModelFilterSet`. This function takes a queryset and can perform arbitrary operations on it and return it. A common use-case is to search for the given search value in multiple fields:
61+
62+
```python
63+
from django.db.models import Q
64+
from netbox.filtersets import NetBoxModelFilterSet
65+
66+
class MyFilterSet(NetBoxModelFilterSet):
67+
...
68+
def search(self, queryset, name, value):
69+
if not value.strip():
70+
return queryset
71+
return queryset.filter(
72+
Q(name__icontains=value) |
73+
Q(description__icontains=value)
74+
)
75+
```
76+
77+
The `search` method is also used by the `q` filter in `NetBoxModelFilterSet` which in turn is used by the Search field in the filters tab.
78+
5879
## Filter Classes
5980

6081
### TagFilter

0 commit comments

Comments
 (0)