Netbox: Only show valid platforms when editing a device

Created on 30 Dec 2019  路  3Comments  路  Source: netbox-community/netbox

Environment

  • Python version: 3.6.8
  • NetBox version: 2.6.9

Proposed Functionality

When editing a device, only show valid values for the "Platform" field, i.e. such platforms that either have a) no manufacturer or b) the manufacturer of the device assigned.

Use Case

Currently, the user is presented with a list of all platforms defined in Netbox. When selecting a platform that has a manufacturer different to that of the device, the following message appears after clicking on "Update":
netbox_platform

Limiting the drop down menu to valid values only makes it easier to find the correct entry when dealing with a large number of platforms and avoids the above error message in the first place.

Database Changes

none

External Dependencies

none

accepted bug

Most helpful comment

Looking into this further, we should be able to use additional_query_params. Just need to modify the Javscript to allow for redundant query values.

All 3 comments

I can do the "manufacturer of the device" bit (below), but I'm not sure how additionally list the platforms with no manufacturers.

--- a/netbox/dcim/forms.py
+++ b/netbox/dcim/forms.py
@@ -1339,7 +1339,8 @@ class DeviceForm(BootstrapMixin, TenancyForm, CustomFieldForm):
         widget=APISelect(
             api_url="/api/dcim/manufacturers/",
             filter_for={
-                'device_type': 'manufacturer_id'
+                'device_type': 'manufacturer_id',
+                'platform': 'manufacturer_id'
             }
         )
     )

A platform with no manufacturer is currently assignable to a device with a specific platform, so the above will stop that as it will filter out any platforms with no manufacturers, unless the user picks the device's platform (no manufacturer) first, then the device's manufacturer (a bit janky).

Looks like we've worked ourselves into a corner here. The current implementation only allows for filter a) a specific value, or b) null, if the filtering field is nullable. However, manufacturer is required and thus not nullable. (But even if it was, we still want to match on $value OR null.)

The least disruptive solution might be to allow appending +null to the filter_for value and treating it as a special condition. For example:

filter_for={
    'device_type': 'manufacturer_id',
    'platform': 'manufacturer_id+null'
}

Modifying the applicable Javascript would result in the API call GET /api/dcim/platforms/?manufacturer_id=123&manufacturer_id=null (where 123 is the value of the manufacturer field).

I'll take a shot at this and see if it works.

Looking into this further, we should be able to use additional_query_params. Just need to modify the Javscript to allow for redundant query values.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robbagithub picture robbagithub  路  3Comments

cloos picture cloos  路  3Comments

markve-sa picture markve-sa  路  4Comments

benjy44 picture benjy44  路  3Comments

bellwood picture bellwood  路  3Comments