44import django_filters
55from django .db .models import Q
66from nautobot .apps .filters import (
7- BaseFilterSet ,
8- CreatedUpdatedModelFilterSetMixin ,
9- CustomFieldModelFilterSetMixin ,
107 NaturalKeyOrPKMultipleChoiceFilter ,
118 NautobotFilterSet ,
129 RoleModelFilterSetMixin ,
1310 SearchFilter ,
1411 StatusModelFilterSetMixin ,
1512)
13+ from nautobot .circuits .models import Provider
1614from nautobot .dcim .models import Device
1715from nautobot .extras .models import Role
1816from nautobot .ipam .models import VRF
17+ from nautobot .tenancy .models import Tenant
1918
2019from . import choices , models
2120
@@ -29,6 +28,13 @@ class AutonomousSystemFilterSet(NautobotFilterSet, StatusModelFilterSetMixin):
2928 "description" : "icontains" ,
3029 },
3130 )
31+
32+ provider = NaturalKeyOrPKMultipleChoiceFilter (
33+ queryset = Provider .objects .all (),
34+ label = "Provider (name or ID)" ,
35+ to_field_name = "name" ,
36+ )
37+
3238 autonomous_system_range = django_filters .ModelMultipleChoiceFilter (
3339 queryset = models .AutonomousSystemRange .objects .all (),
3440 label = "ASN Range" ,
@@ -37,7 +43,7 @@ class AutonomousSystemFilterSet(NautobotFilterSet, StatusModelFilterSetMixin):
3743
3844 class Meta :
3945 model = models .AutonomousSystem
40- fields = [ "id" , "asn" , "status" , "tags" , "autonomous_system_range" ]
46+ fields = "__all__"
4147
4248 def filter_present_in_asn_range (self , queryset , name , value ): # pylint: disable=unused-argument
4349 """Filter Autonomous Systems that are present in any of the given ASN Ranges."""
@@ -61,9 +67,15 @@ class AutonomousSystemRangeFilterSet(NautobotFilterSet):
6167 },
6268 )
6369
70+ tenant = NaturalKeyOrPKMultipleChoiceFilter (
71+ queryset = Tenant .objects .all (),
72+ to_field_name = "name" ,
73+ label = "Tenant (name or ID)" ,
74+ )
75+
6476 class Meta :
6577 model = models .AutonomousSystemRange
66- fields = [ "id" , "name" , "asn_min" , "asn_max" , "tags" ]
78+ fields = "__all__"
6779
6880
6981class BGPRoutingInstanceFilterSet (NautobotFilterSet , StatusModelFilterSetMixin ):
@@ -82,21 +94,21 @@ class BGPRoutingInstanceFilterSet(NautobotFilterSet, StatusModelFilterSetMixin):
8294 label = "Autonomous System Number" ,
8395 )
8496
97+ # TODO: Remove this filter. Deprecated in favor of below NaturalKeyOrPKMultipleChoiceFilter `device`
8598 device_id = django_filters .ModelMultipleChoiceFilter (
8699 queryset = Device .objects .all (),
87100 label = "Device (ID)" ,
88101 )
89102
90- device = django_filters .ModelMultipleChoiceFilter (
91- field_name = "device__name" ,
103+ device = NaturalKeyOrPKMultipleChoiceFilter (
92104 queryset = Device .objects .all (),
93105 to_field_name = "name" ,
94- label = "Device (name)" ,
106+ label = "Device (name or ID )" ,
95107 )
96108
97109 class Meta :
98110 model = models .BGPRoutingInstance
99- fields = [ "id" , "autonomous_system" , "tags" ]
111+ fields = "__all__"
100112
101113
102114class PeerGroupFilterSet (NautobotFilterSet , RoleModelFilterSetMixin ):
@@ -123,23 +135,24 @@ class PeerGroupFilterSet(NautobotFilterSet, RoleModelFilterSetMixin):
123135 label = "BGP Routing Instance ID" ,
124136 )
125137
126- device = django_filters .ModelMultipleChoiceFilter (
127- field_name = "routing_instance__device__name" ,
128- queryset = Device .objects .all (),
129- to_field_name = "name" ,
130- label = "Device (name)" ,
131- )
132-
138+ # TODO: Remove this filter. Deprecated in favor of below NaturalKeyOrPKMultipleChoiceFilter `device`
133139 device_id = django_filters .ModelMultipleChoiceFilter (
134140 field_name = "routing_instance__device__id" ,
135141 queryset = Device .objects .all (),
136142 to_field_name = "id" ,
137143 label = "Device (ID)" ,
138144 )
139145
146+ device = NaturalKeyOrPKMultipleChoiceFilter (
147+ field_name = "routing_instance__device" ,
148+ queryset = Device .objects .all (),
149+ to_field_name = "name" ,
150+ label = "Device (name or ID)" ,
151+ )
152+
140153 class Meta :
141154 model = models .PeerGroup
142- fields = [ "id" , "name" , "enabled" ]
155+ fields = "__all__"
143156
144157
145158class PeerGroupTemplateFilterSet (NautobotFilterSet , RoleModelFilterSetMixin ):
@@ -161,7 +174,7 @@ class PeerGroupTemplateFilterSet(NautobotFilterSet, RoleModelFilterSetMixin):
161174
162175 class Meta :
163176 model = models .PeerGroupTemplate
164- fields = [ "id" , "name" , "enabled" ]
177+ fields = "__all__"
165178
166179
167180class PeerEndpointFilterSet (NautobotFilterSet , RoleModelFilterSetMixin ):
@@ -174,43 +187,40 @@ class PeerEndpointFilterSet(NautobotFilterSet, RoleModelFilterSetMixin):
174187 },
175188 )
176189
177- device = django_filters .ModelMultipleChoiceFilter (
178- field_name = "routing_instance__device__name" ,
179- queryset = Device .objects .all (),
180- to_field_name = "name" ,
181- label = "Device (name)" ,
182- )
183-
190+ # TODO: Remove this filter. Deprecated in favor of below NaturalKeyOrPKMultipleChoiceFilter `device`
184191 device_id = django_filters .ModelMultipleChoiceFilter (
185192 field_name = "routing_instance__device__id" ,
186193 queryset = Device .objects .all (),
187194 to_field_name = "id" ,
188195 label = "Device (ID)" ,
189196 )
190197
198+ device = NaturalKeyOrPKMultipleChoiceFilter (
199+ field_name = "routing_instance__device" ,
200+ queryset = Device .objects .all (),
201+ to_field_name = "name" ,
202+ label = "Device (name or ID)" ,
203+ )
204+
191205 autonomous_system = django_filters .ModelMultipleChoiceFilter (
192206 field_name = "autonomous_system__asn" ,
193207 queryset = models .AutonomousSystem .objects .all (),
194208 to_field_name = "asn" ,
195209 label = "Autonomous System Number" ,
196210 )
197211
198- peer_group = django_filters . ModelMultipleChoiceFilter (
212+ peer_group = NaturalKeyOrPKMultipleChoiceFilter (
199213 queryset = models .PeerGroup .objects .all (),
200- label = "Peer Group (id)" ,
214+ to_field_name = "name" ,
215+ label = "Peer Group (name or ID)" ,
201216 )
202217
203218 class Meta :
204219 model = models .PeerEndpoint
205- fields = [ "id" , "enabled" ]
220+ fields = "__all__"
206221
207222
208- class PeeringFilterSet (
209- BaseFilterSet ,
210- CreatedUpdatedModelFilterSetMixin ,
211- CustomFieldModelFilterSetMixin ,
212- StatusModelFilterSetMixin ,
213- ):
223+ class PeeringFilterSet (StatusModelFilterSetMixin , NautobotFilterSet ):
214224 """Filtering of Peering records."""
215225
216226 # TODO(mzb): Add in-memory filtering for Provider, ASN, IP Address, ...
@@ -222,20 +232,21 @@ class PeeringFilterSet(
222232 },
223233 )
224234
225- device = django_filters .ModelMultipleChoiceFilter (
226- field_name = "endpoints__routing_instance__device__name" ,
227- queryset = Device .objects .all (),
228- to_field_name = "name" ,
229- label = "Device (name)" ,
230- )
231-
235+ # TODO: Remove this filter. Deprecated in favor of below NaturalKeyOrPKMultipleChoiceFilter `device`
232236 device_id = django_filters .ModelMultipleChoiceFilter (
233237 field_name = "endpoints__routing_instance__device__id" ,
234238 queryset = Device .objects .all (),
235239 to_field_name = "id" ,
236240 label = "Device (ID)" ,
237241 )
238242
243+ device = NaturalKeyOrPKMultipleChoiceFilter (
244+ field_name = "endpoints__routing_instance__device" ,
245+ queryset = Device .objects .all (),
246+ to_field_name = "name" ,
247+ label = "Device (name or ID)" ,
248+ )
249+
239250 device_role = django_filters .ModelMultipleChoiceFilter (
240251 field_name = "endpoints__routing_instance__device__role__name" ,
241252 queryset = Role .objects .all (),
@@ -252,10 +263,10 @@ class PeeringFilterSet(
252263
253264 class Meta :
254265 model = models .Peering
255- fields = [ "id" ]
266+ fields = "__all__"
256267
257268
258- class AddressFamilyFilterSet (BaseFilterSet , CreatedUpdatedModelFilterSetMixin , CustomFieldModelFilterSetMixin ):
269+ class AddressFamilyFilterSet (NautobotFilterSet ):
259270 """Filtering of AddressFamily records."""
260271
261272 q = SearchFilter (
@@ -281,15 +292,10 @@ class AddressFamilyFilterSet(BaseFilterSet, CreatedUpdatedModelFilterSetMixin, C
281292
282293 class Meta :
283294 model = models .AddressFamily
284- fields = [
285- "id" ,
286- "routing_instance" ,
287- "afi_safi" ,
288- "vrf" ,
289- ]
295+ fields = "__all__"
290296
291297
292- class PeerGroupAddressFamilyFilterSet (BaseFilterSet , CreatedUpdatedModelFilterSetMixin , CustomFieldModelFilterSetMixin ):
298+ class PeerGroupAddressFamilyFilterSet (NautobotFilterSet ):
293299 """Filtering of PeerGroupAddressFamily records."""
294300
295301 q = SearchFilter (
@@ -302,23 +308,18 @@ class PeerGroupAddressFamilyFilterSet(BaseFilterSet, CreatedUpdatedModelFilterSe
302308
303309 afi_safi = django_filters .MultipleChoiceFilter (choices = choices .AFISAFIChoices )
304310
305- peer_group = django_filters . ModelMultipleChoiceFilter (
306- label = "Peer Group (ID)" ,
311+ peer_group = NaturalKeyOrPKMultipleChoiceFilter (
312+ label = "Peer Group (name or ID)" ,
307313 queryset = models .PeerGroup .objects .all (),
314+ to_field_name = "name" ,
308315 )
309316
310317 class Meta :
311318 model = models .PeerGroupAddressFamily
312- fields = [
313- "id" ,
314- "afi_safi" ,
315- "peer_group" ,
316- ]
319+ fields = "__all__"
317320
318321
319- class PeerEndpointAddressFamilyFilterSet (
320- BaseFilterSet , CreatedUpdatedModelFilterSetMixin , CustomFieldModelFilterSetMixin
321- ):
322+ class PeerEndpointAddressFamilyFilterSet (NautobotFilterSet ):
322323 """Filtering of PeerEndpointAddressFamily records."""
323324
324325 q = SearchFilter (
@@ -338,8 +339,4 @@ class PeerEndpointAddressFamilyFilterSet(
338339
339340 class Meta :
340341 model = models .PeerEndpointAddressFamily
341- fields = [
342- "id" ,
343- "afi_safi" ,
344- "peer_endpoint" ,
345- ]
342+ fields = "__all__"
0 commit comments