Repository: netbox-community/netbox

Issue:
Filtering providers by AS number does not work
### Deployment Type

NetBox Cloud

### NetBox Version

v3.7.4

### Python Version

3.11

### Steps to Reproduce

1. Create an ASN with number 65001.
2. Create a provider and assign it the ASN.
3. Navigate to the providers list in the UI and select the "Filters" tab.
4. Enter 65001 in the "ASN" field and submit.

### Expected Behavior

Only the provider(s) to which the ASN has been assigned should be included in the filtered list.

### Observed Behavior

The filter has no effect: all providers are included in the list. This is because ProviderFilterSet no longer has a filter for the legacy `asn` field, which was removed from the Provider model in NetBox v3.4.

The ASN field should be updated to use the `asn_id` filter, and should present the user with a dynamic selection field to choose from the existing ASN objects.


Discussion:

Resolving patch:
diff --git a/netbox/circuits/filtersets.py b/netbox/circuits/filtersets.py
--- a/netbox/circuits/filtersets.py
+++ b/netbox/circuits/filtersets.py
@@ -64,6 +64,12 @@ class ProviderFilterSet(NetBoxModelFilterSet, ContactModelFilterSet):
         queryset=ASN.objects.all(),
         label=_('ASN (ID)'),
     )
+    asn = django_filters.ModelMultipleChoiceFilter(
+        field_name='asns__asn',
+        queryset=ASN.objects.all(),
+        to_field_name='asn',
+        label=_('ASN'),
+    )
 
     class Meta:
         model = Provider
diff --git a/netbox/circuits/forms/filtersets.py b/netbox/circuits/forms/filtersets.py
--- a/netbox/circuits/forms/filtersets.py
+++ b/netbox/circuits/forms/filtersets.py
@@ -24,7 +24,7 @@ class ProviderFilterForm(ContactModelFilterForm, NetBoxModelFilterSetForm):
     fieldsets = (
         (None, ('q', 'filter_id', 'tag')),
         (_('Location'), ('region_id', 'site_group_id', 'site_id')),
-        (_('ASN'), ('asn',)),
+        (_('ASN'), ('asn_id',)),
         (_('Contacts'), ('contact', 'contact_role', 'contact_group')),
     )
     region_id = DynamicModelMultipleChoiceField(
@@ -46,10 +46,6 @@ class ProviderFilterForm(ContactModelFilterForm, NetBoxModelFilterSetForm):
         },
         label=_('Site')
     )
-    asn = forms.IntegerField(
-        required=False,
-        label=_('ASN (legacy)')
-    )
     asn_id = DynamicModelMultipleChoiceField(
         queryset=ASN.objects.all(),
         required=False,


Test patch:
diff --git a/netbox/circuits/tests/test_filtersets.py b/netbox/circuits/tests/test_filtersets.py
--- a/netbox/circuits/tests/test_filtersets.py
+++ b/netbox/circuits/tests/test_filtersets.py
@@ -90,10 +90,12 @@ def test_description(self):
         params = {'description': ['foobar1', 'foobar2']}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
-    def test_asn_id(self):  # ASN object assignment
+    def test_asn(self):
         asns = ASN.objects.all()[:2]
         params = {'asn_id': [asns[0].pk, asns[1].pk]}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
+        params = {'asn': [asns[0].asn, asns[1].asn]}
+        self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
     def test_region(self):
         regions = Region.objects.all()[:2]