mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
This commit is contained in:
parent
f96df73093
commit
5d7ed871f2
@ -61,6 +61,11 @@ class MyModelViewSet(...):
|
|||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
This class filters `tags` using the `slug` field. For example:
|
||||||
|
|
||||||
|
`GET /api/dcim/sites/?tag=alpha&tag=bravo`
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from django_filters import FilterSet
|
from django_filters import FilterSet
|
||||||
from extras.filters import TagFilter
|
from extras.filters import TagFilter
|
||||||
@ -68,3 +73,19 @@ from extras.filters import TagFilter
|
|||||||
class MyModelFilterSet(FilterSet):
|
class MyModelFilterSet(FilterSet):
|
||||||
tag = TagFilter()
|
tag = TagFilter()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### TagIDFilter
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
This class filters `tags` using the `id` field. For example:
|
||||||
|
|
||||||
|
`GET /api/dcim/sites/?tag_id=100&tag_id=200`
|
||||||
|
|
||||||
|
```python
|
||||||
|
from django_filters import FilterSet
|
||||||
|
from extras.filters import TagIDFilter
|
||||||
|
|
||||||
|
class MyModelFilterSet(FilterSet):
|
||||||
|
tag_id = TagIDFilter()
|
||||||
|
```
|
||||||
|
@ -4,6 +4,7 @@ from .models import Tag
|
|||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'TagFilter',
|
'TagFilter',
|
||||||
|
'TagIDFilter',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -20,3 +21,18 @@ class TagFilter(django_filters.ModelMultipleChoiceFilter):
|
|||||||
kwargs.setdefault('queryset', Tag.objects.all())
|
kwargs.setdefault('queryset', Tag.objects.all())
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class TagIDFilter(django_filters.ModelMultipleChoiceFilter):
|
||||||
|
"""
|
||||||
|
Match on one or more assigned tags. If multiple tags are specified (e.g. ?tag=1&tag=2), the queryset is filtered
|
||||||
|
to objects matching all tags.
|
||||||
|
"""
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
||||||
|
kwargs.setdefault('field_name', 'tags__id')
|
||||||
|
kwargs.setdefault('to_field_name', 'id')
|
||||||
|
kwargs.setdefault('conjoined', True)
|
||||||
|
kwargs.setdefault('queryset', Tag.objects.all())
|
||||||
|
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
@ -13,7 +13,7 @@ from utilities.filters import (
|
|||||||
)
|
)
|
||||||
from virtualization.models import Cluster, ClusterGroup, ClusterType
|
from virtualization.models import Cluster, ClusterGroup, ClusterType
|
||||||
from .choices import *
|
from .choices import *
|
||||||
from .filters import TagFilter
|
from .filters import TagFilter, TagIDFilter
|
||||||
from .models import *
|
from .models import *
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -758,6 +758,7 @@ class ConfigTemplateFilterSet(ChangeLoggedModelFilterSet):
|
|||||||
label=_('Data file (ID)'),
|
label=_('Data file (ID)'),
|
||||||
)
|
)
|
||||||
tag = TagFilter()
|
tag = TagFilter()
|
||||||
|
tag_id = TagIDFilter()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ConfigTemplate
|
model = ConfigTemplate
|
||||||
|
@ -12,7 +12,7 @@ from django.utils.translation import gettext as _
|
|||||||
from core.choices import ObjectChangeActionChoices
|
from core.choices import ObjectChangeActionChoices
|
||||||
from core.models import ObjectChange
|
from core.models import ObjectChange
|
||||||
from extras.choices import CustomFieldFilterLogicChoices
|
from extras.choices import CustomFieldFilterLogicChoices
|
||||||
from extras.filters import TagFilter
|
from extras.filters import TagFilter, TagIDFilter
|
||||||
from extras.models import CustomField, SavedFilter
|
from extras.models import CustomField, SavedFilter
|
||||||
from utilities.constants import (
|
from utilities.constants import (
|
||||||
FILTER_CHAR_BASED_LOOKUP_MAP, FILTER_NEGATION_LOOKUP_MAP, FILTER_TREENODE_NEGATION_LOOKUP_MAP,
|
FILTER_CHAR_BASED_LOOKUP_MAP, FILTER_NEGATION_LOOKUP_MAP, FILTER_TREENODE_NEGATION_LOOKUP_MAP,
|
||||||
@ -289,6 +289,7 @@ class NetBoxModelFilterSet(ChangeLoggedModelFilterSet):
|
|||||||
label=_('Search'),
|
label=_('Search'),
|
||||||
)
|
)
|
||||||
tag = TagFilter()
|
tag = TagFilter()
|
||||||
|
tag_id = TagIDFilter()
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user