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:
@@ -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
|
||||
#
|
||||
|
Reference in New Issue
Block a user