From 44cb1a913957329c24554094b782b894a7410dec Mon Sep 17 00:00:00 2001 From: Martin Hauser Date: Tue, 15 Apr 2025 14:22:59 +0200 Subject: [PATCH] =?UTF-8?q?Fixes=20#19056=20=E2=80=93=20Add=20Device=20fil?= =?UTF-8?q?tering=20by=20Location=20slug=20(#19180)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(dcim): Add filter by location slug for Device Introduces a TreeNodeMultipleChoiceFilter for filtering locations by slug. Enhances filtering flexibility in the Device model by supporting both ID and slug lookups. Fixes #19056 * feat(dcim): Add Device filtering by location slug in tests Extend test cases to include filtering by location slug. Ensures the FilterSet works correctly with slug-based queries for locations. Fixes #19056 --- netbox/dcim/filtersets.py | 7 +++++++ netbox/dcim/tests/test_filtersets.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/netbox/dcim/filtersets.py b/netbox/dcim/filtersets.py index fcb3c7e50..636a4b6be 100644 --- a/netbox/dcim/filtersets.py +++ b/netbox/dcim/filtersets.py @@ -1057,6 +1057,13 @@ class DeviceFilterSet( lookup_expr='in', label=_('Location (ID)'), ) + location = TreeNodeMultipleChoiceFilter( + queryset=Location.objects.all(), + field_name='location', + lookup_expr='in', + to_field_name='slug', + label=_('Location (slug)'), + ) rack_id = django_filters.ModelMultipleChoiceFilter( field_name='rack', queryset=Rack.objects.all(), diff --git a/netbox/dcim/tests/test_filtersets.py b/netbox/dcim/tests/test_filtersets.py index ede1e2a09..a938e14c0 100644 --- a/netbox/dcim/tests/test_filtersets.py +++ b/netbox/dcim/tests/test_filtersets.py @@ -2561,6 +2561,8 @@ class DeviceTestCase(TestCase, ChangeLoggedFilterSetTests): locations = Location.objects.all()[:2] params = {'location_id': [locations[0].pk, locations[1].pk]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + params = {'location': [locations[0].slug, locations[1].slug]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) def test_rack(self): racks = Rack.objects.all()[:2]