Просмотр исходного кода

Fixes #12401: Support the creation of front ports without a pre-populated device ID

jeremystretch 2 лет назад
Родитель
Сommit
1952707702
2 измененных файлов с 18 добавлено и 3 удалено
  1. 1 0
      docs/release-notes/version-3.5.md
  2. 17 3
      netbox/dcim/forms/object_create.py

+ 1 - 0
docs/release-notes/version-3.5.md

@@ -8,6 +8,7 @@
 * [#12384](https://github.com/netbox-community/netbox/issues/12384) - Add a three-second timeout for RSS reader widget
 * [#12395](https://github.com/netbox-community/netbox/issues/12395) - Fix "create & add another" action for objects with custom fields
 * [#12396](https://github.com/netbox-community/netbox/issues/12396) - Provider account should not be a required field in REST API serializer
+* [#12401](https://github.com/netbox-community/netbox/issues/12401) - Support the creation of front ports without a pre-populated device ID
 * [#12405](https://github.com/netbox-community/netbox/issues/12405) - Fix filtering for VLAN groups displayed under site view
 * [#12412](https://github.com/netbox-community/netbox/issues/12412) - Device/VM interface MAC addresses can be nullified via REST API
 * [#12415](https://github.com/netbox-community/netbox/issues/12415) - Pin `rq` to v1.13.0 to fix `ImportError` exception from `django-rq`

+ 17 - 3
netbox/dcim/forms/object_create.py

@@ -4,6 +4,7 @@ from django.utils.translation import gettext as _
 from dcim.models import *
 from netbox.forms import NetBoxModelForm
 from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField, ExpandableNameField
+from utilities.forms.widgets import APISelect
 from . import model_forms
 
 __all__ = (
@@ -225,6 +226,18 @@ class InterfaceCreateForm(ComponentCreateForm, model_forms.InterfaceForm):
 
 
 class FrontPortCreateForm(ComponentCreateForm, model_forms.FrontPortForm):
+    device = DynamicModelChoiceField(
+        queryset=Device.objects.all(),
+        selector=True,
+        widget=APISelect(
+            # TODO: Clean up the application of HTMXSelect attributes
+            attrs={
+                'hx-get': '.',
+                'hx-include': f'#form_fields',
+                'hx-target': f'#form_fields',
+            }
+        )
+    )
     rear_port = forms.MultipleChoiceField(
         choices=[],
         label=_('Rear ports'),
@@ -244,9 +257,10 @@ class FrontPortCreateForm(ComponentCreateForm, model_forms.FrontPortForm):
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
 
-        device = Device.objects.get(
-            pk=self.initial.get('device') or self.data.get('device')
-        )
+        if device_id := self.data.get('device') or self.initial.get('device'):
+            device = Device.objects.get(pk=device_id)
+        else:
+            return
 
         # Determine which rear port positions are occupied. These will be excluded from the list of available
         # mappings.