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/plugins/development/filtersets.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,27 @@ class MyModelViewSet(...):
55
55
filterset_class = filtersets.MyModelFilterSet
56
56
```
57
57
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
+
classMyFilterSet(NetBoxModelFilterSet):
67
+
...
68
+
defsearch(self, queryset, name, value):
69
+
ifnot 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.
0 commit comments