Repository: netbox-community/netbox
Pull request: 10902 add location to power feed form
<!--
    Thank you for your interest in contributing to NetBox! Please note that
    our contribution policy requires that a feature request or bug report be
    approved and assigned prior to opening a pull request. This helps avoid
    waste time and effort on a proposed change that we might not be able to
    accept.

    IF YOUR PULL REQUEST DOES NOT REFERENCE AN ISSUE WHICH HAS BEEN ASSIGNED
    TO YOU, IT WILL BE CLOSED AUTOMATICALLY.

    Please specify your assigned issue number on the line below.
-->
### Fixes: #10902 

<!--
    Please include a summary of the proposed changes below.
-->
Adds location field to power feed form and filters rack dropdown based on it:

![Monosnap Add a new power feed | NetBox 2022-11-14 09-38-47](https://user-images.githubusercontent.com/99642/201728526-e372f97f-98b8-4f7f-ac5b-ae053240f194.png)


Problem statement:
Add location field to "Add Power Feed" form
### NetBox version

v3.3.5

### Feature type

Change to existing functionality

### Proposed functionality

When creating a new Power Feed via the UI, the form only lets you select a rack, it would be great if the form would also expose a Location selector and then filter the available Racks based on that selection (similar to how the Site choices are limited by a selected Region).

### Use case

We have racks with the same name in the same site but within a different location. That makes it non-obvious which is the right rack to select.

### Database changes

Being able to specify a Location is already possible when using the Bulk Import feature, so we don't think this should require any database changes.

### External dependencies

n/a

Hints from issue discussion:

Commit message: 10902 add location to power feed form

Patch to review:
diff --git a/netbox/dcim/forms/models.py b/netbox/dcim/forms/models.py
index 8f1626361eb..0eb99e8c656 100644
--- a/netbox/dcim/forms/models.py
+++ b/netbox/dcim/forms/models.py
@@ -877,10 +877,18 @@ class PowerFeedForm(NetBoxModelForm):
             'site_id': '$site'
         }
     )
+    location = DynamicModelChoiceField(
+        queryset=Location.objects.all(),
+        required=False,
+        initial_params={
+            'racks': '$rack'
+        }
+    )
     rack = DynamicModelChoiceField(
         queryset=Rack.objects.all(),
         required=False,
         query_params={
+            'location_id': '$location',
             'site_id': '$site'
         }
     )
@@ -888,14 +896,14 @@ class PowerFeedForm(NetBoxModelForm):
 
     fieldsets = (
         ('Power Panel', ('region', 'site', 'power_panel')),
-        ('Power Feed', ('rack', 'name', 'status', 'type', 'mark_connected', 'tags')),
+        ('Power Feed', ('location', 'rack', 'name', 'status', 'type', 'mark_connected', 'tags')),
         ('Characteristics', ('supply', 'voltage', 'amperage', 'phase', 'max_utilization')),
     )
 
     class Meta:
         model = PowerFeed
         fields = [
-            'region', 'site_group', 'site', 'power_panel', 'rack', 'name', 'status', 'type', 'mark_connected', 'supply',
+            'region', 'site_group', 'site', 'power_panel', 'location', 'rack', 'name', 'status', 'type', 'mark_connected', 'supply',
             'phase', 'voltage', 'amperage', 'max_utilization', 'comments', 'tags',
         ]
         widgets = {


Code review comments:
diff_hunk: @@ -877,25 +877,33 @@ class PowerFeedForm(NetBoxModelForm):
             'site_id': '$site'
         }
     )
+    location = DynamicModelChoiceField(
+        queryset=Location.objects.all(),
+        required=False,
+        initial_params={
+            'racks': '$rack'
+        }
path: netbox/dcim/forms/models.py
text: @user1:
Sorry, missed this earlier when we discussed: This should have `query_params` on it too, to limit the locations by the selected site:

```python
        query_params={
            'site_id': '$site'
        }
```