Repository: netbox-community/netbox

Issue:
IPAddress Interface dropdown shows all interfaces, not only device ones
### NetBox version

v3.5.6

### Python version

3.8

### Steps to Reproduce

1. Create several devices with interfaces
2. create an IP address
3. assign the IP to an interface

This IP is now assigned to an interface of a device

1. edit the IP adress
2. open the dropdown of the 'interfaces
![image](https://github.com/netbox-community/netbox/assets/74899468/1333bd83-9029-4dac-a79c-10e63d4c21d4)



### Expected Behavior

The dropdown should only display the interfaces of the device it is assigned to.
If it needs to move to another device, then the 'filter' can be used.

That filter also is not aware of the device the IP/Interface belonged to
![image](https://github.com/netbox-community/netbox/assets/74899468/470fbf94-582a-482e-b2b4-8bb150bb3f75)
It should at least already show the 'Device' field with the device.

### Observed Behavior

All interfaces of all devices are in the dropdown
The filter is not helping a lot, and needs a lot of clicking to get the proper filter setup (Click Filter, Check Device, Click devicefield, Type devicename, Click proper device, click to close the dropdown, click search)


Discussion:
Once PR #13296 is merged. we can piggyback to fix this one too, at least the first part of it.
related: https://github.com/netbox-community/netbox/issues/12751
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
> Once PR #13296 is merged. we can piggyback to fix this one too, at least the first part of it.

@DanSheps now that the above got merged is there anything blocking this?
Marking this as blocked by #12751 for now.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).

Resolving patch:
diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py
--- a/netbox/ipam/forms/model_forms.py
+++ b/netbox/ipam/forms/model_forms.py
@@ -355,6 +355,15 @@ def __init__(self, *args, **kwargs):
             ):
                 self.initial['primary_for_parent'] = True
 
+            if type(instance.assigned_object) is Interface:
+                self.fields['interface'].widget.add_query_params({
+                    'device_id': instance.assigned_object.device.pk,
+                })
+            elif type(instance.assigned_object) is VMInterface:
+                self.fields['vminterface'].widget.add_query_params({
+                    'virtual_machine_id': instance.assigned_object.virtual_machine.pk,
+                })
+
         # Disable object assignment fields if the IP address is designated as primary
         if self.initial.get('primary_for_parent'):
             self.fields['interface'].disabled = True


Test patch: