mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 08:46:10 -06:00
Limit tags by object type during assignment
This commit is contained in:
parent
0dc71d8850
commit
bbcec4ffff
@ -258,10 +258,9 @@ class TagFilterSet(ChangeLoggedModelFilterSet):
|
||||
content_type_id = MultiValueNumberFilter(
|
||||
method='_content_type_id'
|
||||
)
|
||||
object_type_id = MultiValueNumberFilter(
|
||||
field_name='object_types__id'
|
||||
for_object_type_id = MultiValueNumberFilter(
|
||||
method='_for_object_type'
|
||||
)
|
||||
object_types = ContentTypeFilter()
|
||||
|
||||
class Meta:
|
||||
model = Tag
|
||||
@ -302,6 +301,11 @@ class TagFilterSet(ChangeLoggedModelFilterSet):
|
||||
|
||||
return queryset.filter(extras_taggeditem_items__content_type__in=content_types).distinct()
|
||||
|
||||
def _for_object_type(self, queryset, name, values):
|
||||
return queryset.filter(
|
||||
Q(object_types__id__in=values) | Q(object_types__isnull=True)
|
||||
)
|
||||
|
||||
|
||||
class ConfigContextFilterSet(ChangeLoggedModelFilterSet):
|
||||
q = django_filters.CharFilter(
|
||||
|
@ -245,7 +245,7 @@ class TagFilterForm(SavedFiltersMixin, FilterForm):
|
||||
required=False,
|
||||
label=_('Tagged object type')
|
||||
)
|
||||
object_type_id = ContentTypeMultipleChoiceField(
|
||||
for_object_type_id = ContentTypeChoiceField(
|
||||
queryset=ContentType.objects.filter(FeatureQuery('tags').get_query()),
|
||||
required=False,
|
||||
label=_('Allowed object type')
|
||||
|
@ -31,6 +31,13 @@ class NetBoxModelForm(BootstrapMixin, CustomFieldsMixin, forms.ModelForm):
|
||||
required=False
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# Limit tags to those applicable to the object type
|
||||
if ct := self._get_content_type():
|
||||
self.fields['tags'].widget.add_query_param('for_object_type_id', ct.pk)
|
||||
|
||||
def _get_content_type(self):
|
||||
return ContentType.objects.get_for_model(self._meta.model)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user