Skip to content

Commit 9a1d936

Browse files
Fixes: #18783 Add a tag_id filter for all models which support tagging (#18889)
1 parent 64a98fd commit 9a1d936

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

docs/plugins/development/filtersets.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,31 @@ class MyModelViewSet(...):
6161

6262
The `TagFilter` class is available for all models which support tag assignment (those which inherit from `NetBoxModel` or `TagsMixin`). This filter subclasses django-filter's `ModelMultipleChoiceFilter` to work with NetBox's `TaggedItem` class.
6363

64+
This class filters `tags` using the `slug` field. For example:
65+
66+
`GET /api/dcim/sites/?tag=alpha&tag=bravo`
67+
68+
6469
```python
6570
from django_filters import FilterSet
6671
from extras.filters import TagFilter
6772

6873
class MyModelFilterSet(FilterSet):
6974
tag = TagFilter()
7075
```
76+
77+
### TagIDFilter
78+
79+
The `TagIDFilter` class is available for all models which support tag assignment (those which inherit from `NetBoxModel` or `TagsMixin`). This filter subclasses django-filter's `ModelMultipleChoiceFilter` to work with NetBox's `TaggedItem` class.
80+
81+
This class filters `tags` using the `id` field. For example:
82+
83+
`GET /api/dcim/sites/?tag_id=100&tag_id=200`
84+
85+
```python
86+
from django_filters import FilterSet
87+
from extras.filters import TagIDFilter
88+
89+
class MyModelFilterSet(FilterSet):
90+
tag_id = TagIDFilter()
91+
```

netbox/extras/filters.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
__all__ = (
66
'TagFilter',
7+
'TagIDFilter',
78
)
89

910

@@ -20,3 +21,18 @@ def __init__(self, *args, **kwargs):
2021
kwargs.setdefault('queryset', Tag.objects.all())
2122

2223
super().__init__(*args, **kwargs)
24+
25+
26+
class TagIDFilter(django_filters.ModelMultipleChoiceFilter):
27+
"""
28+
Match on one or more assigned tags. If multiple tags are specified (e.g. ?tag=1&tag=2), the queryset is filtered
29+
to objects matching all tags.
30+
"""
31+
def __init__(self, *args, **kwargs):
32+
33+
kwargs.setdefault('field_name', 'tags__id')
34+
kwargs.setdefault('to_field_name', 'id')
35+
kwargs.setdefault('conjoined', True)
36+
kwargs.setdefault('queryset', Tag.objects.all())
37+
38+
super().__init__(*args, **kwargs)

netbox/extras/filtersets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from utilities.filters import ContentTypeFilter, MultiValueCharFilter, MultiValueNumberFilter
1212
from virtualization.models import Cluster, ClusterGroup, ClusterType
1313
from .choices import *
14-
from .filters import TagFilter
14+
from .filters import TagFilter, TagIDFilter
1515
from .models import *
1616

1717
__all__ = (
@@ -665,6 +665,7 @@ class ConfigTemplateFilterSet(ChangeLoggedModelFilterSet):
665665
label=_('Data file (ID)'),
666666
)
667667
tag = TagFilter()
668+
tag_id = TagIDFilter()
668669

669670
class Meta:
670671
model = ConfigTemplate

netbox/netbox/filtersets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from core.choices import ObjectChangeActionChoices
1111
from core.models import ObjectChange
1212
from extras.choices import CustomFieldFilterLogicChoices
13-
from extras.filters import TagFilter
13+
from extras.filters import TagFilter, TagIDFilter
1414
from extras.models import CustomField, SavedFilter
1515
from utilities.constants import (
1616
FILTER_CHAR_BASED_LOOKUP_MAP, FILTER_NEGATION_LOOKUP_MAP, FILTER_TREENODE_NEGATION_LOOKUP_MAP,
@@ -286,6 +286,7 @@ class NetBoxModelFilterSet(ChangeLoggedModelFilterSet):
286286
label=_('Search'),
287287
)
288288
tag = TagFilter()
289+
tag_id = TagIDFilter()
289290

290291
def __init__(self, *args, **kwargs):
291292
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)