From eeede8e5d51820a4ce9cd1d50076c63f45ce5749 Mon Sep 17 00:00:00 2001 From: Johannes Erwerle Date: Sat, 4 Oct 2025 20:55:06 +0200 Subject: [PATCH 1/4] Documented ObjectListView quick search feature for plugins Closes #20449 --- docs/plugins/development/filtersets.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/plugins/development/filtersets.md b/docs/plugins/development/filtersets.md index e19b3a73305..1dedd10a3e9 100644 --- a/docs/plugins/development/filtersets.md +++ b/docs/plugins/development/filtersets.md @@ -55,6 +55,28 @@ class MyModelViewSet(...): filterset_class = filtersets.MyModelFilterSet ``` +### Search function for use by the ObjectListView quick search + +The `ObjectListView` has a field called Quick Search. +For this field to work the corresponding FilterSet has to implement the `search` function. +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: + +```python +from django.db.models import Q +from netbox.filtersets import NetBoxModelFilterSet + +class MyFilterSet(NetBoxModelFilterSet): + ... + def search(self, queryset, name, value): + if not value.strip(): + return queryset + return queryset.filter( + Q(name__icontains=value) | + Q(description__icontains=value) + ) +``` + ## Filter Classes ### TagFilter From 51039a70c4632b99523e7a196b0cd2d5dea50e0e Mon Sep 17 00:00:00 2001 From: Jo Date: Tue, 21 Oct 2025 12:48:55 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Jason Novinger --- docs/plugins/development/filtersets.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/plugins/development/filtersets.md b/docs/plugins/development/filtersets.md index 1dedd10a3e9..227185034c1 100644 --- a/docs/plugins/development/filtersets.md +++ b/docs/plugins/development/filtersets.md @@ -55,10 +55,10 @@ class MyModelViewSet(...): filterset_class = filtersets.MyModelFilterSet ``` -### Search function for use by the ObjectListView quick search +### Implementing Quick Search The `ObjectListView` has a field called Quick Search. -For this field to work the corresponding FilterSet has to implement the `search` function. +For Quick Search to work the corresponding FilterSet has to implement the `search` method. 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: From 6593f1b4e0f4c2d042b31095ed7858a8d7932c1a Mon Sep 17 00:00:00 2001 From: Johannes Erwerle Date: Tue, 21 Oct 2025 13:29:46 +0200 Subject: [PATCH 3/4] improved Quick Search documentation --- docs/plugins/development/filtersets.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/plugins/development/filtersets.md b/docs/plugins/development/filtersets.md index 227185034c1..fc552375c45 100644 --- a/docs/plugins/development/filtersets.md +++ b/docs/plugins/development/filtersets.md @@ -58,7 +58,7 @@ class MyModelViewSet(...): ### Implementing Quick Search The `ObjectListView` has a field called Quick Search. -For Quick Search to work the corresponding FilterSet has to implement the `search` method. +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: @@ -77,6 +77,8 @@ class MyFilterSet(NetBoxModelFilterSet): ) ``` +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. + ## Filter Classes ### TagFilter From 5f4de9b1a94047760ac765c87ab154ba196e86e4 Mon Sep 17 00:00:00 2001 From: Johannes Erwerle Date: Tue, 21 Oct 2025 20:10:12 +0200 Subject: [PATCH 4/4] Changed the documentation from multiple lines to a single line --- docs/plugins/development/filtersets.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/plugins/development/filtersets.md b/docs/plugins/development/filtersets.md index fc552375c45..b0b38027161 100644 --- a/docs/plugins/development/filtersets.md +++ b/docs/plugins/development/filtersets.md @@ -57,10 +57,7 @@ class MyModelViewSet(...): ### Implementing Quick Search -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: +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: ```python from django.db.models import Q