Repository: nautobot/nautobot

Issue:
Bug: VM Interface vlan list in UI uses invalid option
<!--
    NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED.

    This form is only for reporting reproducible bugs. If you need assistance
    with Nautobot installation, or if you have a general question, please start a
    discussion instead: https://github.com/nautobot/nautobot/discussions

    Please describe the environment in which you are running Nautobot. Be sure
    that you are running an unmodified instance of the latest stable release
    before submitting a bug report, and that any plugins have been disabled.
-->
### Environment
* Nautobot version (Docker tag too if applicable): 2.1.0
* Python version: 3.10
* Database platform, version: postgres, 14.10
* Middleware(s):

<!--
    Describe in detail the exact steps that someone else can take to reproduce
    this bug using the current stable release of Nautobot. Begin with the
    creation of any necessary database objects and call out every operation
    being performed explicitly. If reporting a bug in the REST API, be sure to
    reconstruct the raw HTTP request(s) being made: Don't rely on a client
    library such as pynautobot.
-->
### Steps to Reproduce
1. Create a VLAN
2. Create a Cluster Type
3. Create a Cluster
4. Create a VM in the cluster
5. Create a interface on the vm
6. Selected Access, Tagged or Tagged (all)

<!-- What did you expect to happen? -->
### Expected Behavior
Show a list of vlans


<!-- What happened instead? -->
### Observed Behavior
Dropdown shows "The results could not be loaded"

Was able to replicate on [demo.nautobot.com](https://demo.nautobot.com)

It appears that this dropdown tries to use an API query attribute/option called `location_id` however this seems to generate an 400. 

Looking at the API browser with `GET /api/ipam/vlans/?q=&limit=50&offset=0&depth=0&location_id=<<GUID>>` or even `GET /api/ipam/vlans/?q=&limit=50&offset=0&depth=0&location_id=null` returns 

```
HTTP 400 Bad Request
API-Version: 2.1
Allow: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "location_id": [
        "Unknown filter field"
    ]
}
```


Discussion:
Thanks for the report! Looks like there are quite a few cases in `nautobot/virtualization/forms.py` that are using `location_id` as a filter parameter, which should generally be `location` instead.
Hi @glennmatthews, still very very new to the model, but is it just a matter of the references under query_params being changed from
```  
        query_params={
            "location_id": "null",
        },
``` 
to 
```  
        query_params={
            "location": "null",
        },
``` 
?

If so, I'll give it a crack and if it pass any local linting & tests, follow the contributing guide to submit a PR if it will help. If there's more to it, I'll keep out of the way :D
That should be the gist of it, yes. Thank you!
Hello.
I still have the problem on 2.1.3
It seems the wrong filter is still there
```
[Tue Feb  6 09:35:49 2024] GET /api/ipam/vlans/?q=&limit=50&offset=0&depth=0&location=null&location_id=e8573f65-41cd-40cc-a192-70515809c689 => generated 187953 bytes in 33 msecs (HTTP/1.1 400) 9 headers in 447 bytes (1 switches on core 0)
```
so `location=null` is fine, but `location_id=<uuid>` fails
Looks like the fix was incomplete - see also analysis at https://github.com/nautobot/nautobot/pull/5124#issuecomment-1907824331. @tlourey were you going to be able/willing to submit a followup PR, or would you need us to run with fixing this?
My apologies for my delay. I'll try and get to this in the next few days.

On Wed, 7 Feb 2024, 12:34 am Glenn Matthews, ***@***.***>
wrote:

> Looks like the fix was incomplete - see also analysis at #5124 (comment)
> <https://github.com/nautobot/nautobot/pull/5124#issuecomment-1907824331>.
> @tlourey <https://github.com/tlourey> were you going to be able/willing
> to submit a followup PR, or would you need us to run with fixing this?
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/nautobot/nautobot/issues/5058#issuecomment-1929631943>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ACH7VAYWKUGNNZK7NPGPQ4LYSIWPTAVCNFSM6AAAAABBPRWKXWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRZGYZTCOJUGM>
> .
> You are receiving this because you were mentioned.Message ID:
> ***@***.***>
>


Resolving patch:
diff --git a/nautobot/virtualization/forms.py b/nautobot/virtualization/forms.py
--- a/nautobot/virtualization/forms.py
+++ b/nautobot/virtualization/forms.py
@@ -461,8 +461,8 @@ def __init__(self, *args, **kwargs):
         # Add current location to VLANs query params
         location = virtual_machine.location
         if location:
-            self.fields["untagged_vlan"].widget.add_query_param("location_id", location.pk)
-            self.fields["tagged_vlans"].widget.add_query_param("location_id", location.pk)
+            self.fields["untagged_vlan"].widget.add_query_param("location", location.pk)
+            self.fields["tagged_vlans"].widget.add_query_param("location", location.pk)
 
 
 class VMInterfaceCreateForm(BootstrapMixin, InterfaceCommonForm):
@@ -534,8 +534,8 @@ def __init__(self, *args, **kwargs):
         # Add current location to VLANs query params
         location = virtual_machine.location
         if location:
-            self.fields["untagged_vlan"].widget.add_query_param("location_id", location.pk)
-            self.fields["tagged_vlans"].widget.add_query_param("location_id", location.pk)
+            self.fields["untagged_vlan"].widget.add_query_param("location", location.pk)
+            self.fields["tagged_vlans"].widget.add_query_param("location", location.pk)
 
 
 class VMInterfaceBulkEditForm(TagsBulkEditFormMixin, StatusModelBulkEditFormMixin, NautobotBulkEditForm):
@@ -604,8 +604,8 @@ def __init__(self, *args, **kwargs):
             location = getattr(parent_obj.cluster, "location", None)
             if location is not None:
                 # Add current location to VLANs query params
-                self.fields["untagged_vlan"].widget.add_query_param("location_id", location.pk)
-                self.fields["tagged_vlans"].widget.add_query_param("location_id", location.pk)
+                self.fields["untagged_vlan"].widget.add_query_param("location", location.pk)
+                self.fields["tagged_vlans"].widget.add_query_param("location", location.pk)
 
         self.fields["parent_interface"].choices = ()
         self.fields["parent_interface"].widget.attrs["disabled"] = True


Test patch: