From 18a516ee5377d0637529fb664620b87d8b5fc576 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 11 Nov 2016 15:29:40 -0500 Subject: [PATCH] Closes #685: When assigning an IP to a device, automaitcally select the interface if only one exists --- netbox/dcim/forms.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/netbox/dcim/forms.py b/netbox/dcim/forms.py index b57e31d1e..44e30e964 100644 --- a/netbox/dcim/forms.py +++ b/netbox/dcim/forms.py @@ -1252,10 +1252,15 @@ class IPAddressForm(BootstrapMixin, CustomFieldForm): self.fields['vrf'].empty_label = 'Global' - self.fields['interface'].queryset = device.interfaces.all() + interfaces = device.interfaces.all() + self.fields['interface'].queryset = interfaces self.fields['interface'].required = True - # If this device does not have any IP addresses assigned, default to setting the first IP as its primary + # If this device has only one interface, select it by default. + if len(interfaces) == 1: + self.fields['interface'].initial = interfaces[0] + + # If this device does not have any IP addresses assigned, default to setting the first IP as its primary. if not IPAddress.objects.filter(interface__device=device).count(): self.fields['set_as_primary'].initial = True