1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #4897: Allow filtering by content type identified as <app>.<model> string

This commit is contained in:
Jeremy Stretch
2020-09-22 16:06:38 -04:00
parent 5ba4252388
commit 0c3fafdfef
5 changed files with 132 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
import django_filters
from django_filters.constants import EMPTY_VALUES
from copy import deepcopy
from dcim.forms import MACAddressField
from django import forms
@@ -115,6 +116,26 @@ class NumericArrayFilter(django_filters.NumberFilter):
return super().filter(qs, value)
class ContentTypeFilter(django_filters.CharFilter):
"""
Allow specifying a ContentType by <app_label>.<model> (e.g. "dcim.site").
"""
def filter(self, qs, value):
if value in EMPTY_VALUES:
return qs
try:
app_label, model = value.lower().split('.')
except ValueError:
return qs.none()
return qs.filter(
**{
f'{self.field_name}__app_label': app_label,
f'{self.field_name}__model': model
}
)
#
# FilterSets
#