|
1 | 1 | import django_filters
|
2 | 2 | from django.contrib.postgres.fields import ArrayField
|
3 |
| -from django.db.models import JSONField |
4 |
| -from netbox.filtersets import BaseFilterSet, NetBoxModelFilterSet |
| 3 | +from django.db.models import JSONField, Q |
| 4 | + |
| 5 | +from extras.choices import CustomFieldTypeChoices |
| 6 | +from netbox.filtersets import NetBoxModelFilterSet |
5 | 7 |
|
6 | 8 | from .models import CustomObjectType
|
7 | 9 |
|
@@ -51,13 +53,28 @@ def get_filterset_class(model):
|
51 | 53 | },
|
52 | 54 | )
|
53 | 55 |
|
| 56 | + def search(self, queryset, name, value): |
| 57 | + if not value.strip(): |
| 58 | + return queryset |
| 59 | + q = Q() |
| 60 | + for field in model.custom_object_type.fields.all(): |
| 61 | + if field.type in [ |
| 62 | + CustomFieldTypeChoices.TYPE_TEXT, |
| 63 | + CustomFieldTypeChoices.TYPE_LONGTEXT, |
| 64 | + CustomFieldTypeChoices.TYPE_JSON, |
| 65 | + CustomFieldTypeChoices.TYPE_URL, |
| 66 | + ]: |
| 67 | + q |= Q(**{f"{field.name}__icontains": value}) |
| 68 | + return queryset.filter(q) |
| 69 | + |
54 | 70 | attrs = {
|
55 | 71 | "Meta": meta,
|
56 | 72 | "__module__": "database.filtersets",
|
| 73 | + "search": search, |
57 | 74 | }
|
58 | 75 |
|
59 | 76 | return type(
|
60 | 77 | f"{model._meta.object_name}FilterSet",
|
61 |
| - (BaseFilterSet,), # TODO: Should be a NetBoxModelFilterSet |
| 78 | + (NetBoxModelFilterSet,), |
62 | 79 | attrs,
|
63 | 80 | )
|
0 commit comments