Jelajahi Sumber

Fixes #9829: Arrange custom fields by group when editing objects

jeremystretch 3 tahun lalu
induk
melakukan
466931d2fb

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

@@ -105,6 +105,7 @@ Custom field UI visibility has no impact on API operation.
 * [#9765](https://github.com/netbox-community/netbox/issues/9765) - Report correct segment count under cable trace UI view
 * [#9765](https://github.com/netbox-community/netbox/issues/9765) - Report correct segment count under cable trace UI view
 * [#9794](https://github.com/netbox-community/netbox/issues/9794) - Fix link to connect a rear port to a circuit termination
 * [#9794](https://github.com/netbox-community/netbox/issues/9794) - Fix link to connect a rear port to a circuit termination
 * [#9818](https://github.com/netbox-community/netbox/issues/9818) - Fix circuit side selection when connecting a cable to a circuit termination
 * [#9818](https://github.com/netbox-community/netbox/issues/9818) - Fix circuit side selection when connecting a cable to a circuit termination
+* [#9829](https://github.com/netbox-community/netbox/issues/9829) - Arrange custom fields by group when editing objects
 * [#9843](https://github.com/netbox-community/netbox/issues/9843) - Fix rendering of custom field values (regression from #9647)
 * [#9843](https://github.com/netbox-community/netbox/issues/9843) - Fix rendering of custom field values (regression from #9647)
 * [#9844](https://github.com/netbox-community/netbox/issues/9844) - Fix interface api request when creating/editing L2VPN termination
 * [#9844](https://github.com/netbox-community/netbox/issues/9844) - Fix interface api request when creating/editing L2VPN termination
 
 

+ 4 - 0
netbox/extras/forms/customfields.py

@@ -19,6 +19,7 @@ class CustomFieldsMixin:
 
 
     def __init__(self, *args, **kwargs):
     def __init__(self, *args, **kwargs):
         self.custom_fields = {}
         self.custom_fields = {}
+        self.custom_field_groups = {}
 
 
         super().__init__(*args, **kwargs)
         super().__init__(*args, **kwargs)
 
 
@@ -58,3 +59,6 @@ class CustomFieldsMixin:
 
 
             # Annotate the field in the list of CustomField form fields
             # Annotate the field in the list of CustomField form fields
             self.custom_fields[field_name] = customfield
             self.custom_fields[field_name] = customfield
+            if customfield.group_name not in self.custom_field_groups:
+                self.custom_field_groups[customfield.group_name] = []
+            self.custom_field_groups[customfield.group_name].append(field_name)

+ 8 - 19
netbox/netbox/forms/base.py

@@ -94,30 +94,19 @@ class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
 
 
     def __init__(self, *args, **kwargs):
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         super().__init__(*args, **kwargs)
+
         self.fields['pk'].queryset = self.model.objects.all()
         self.fields['pk'].queryset = self.model.objects.all()
 
 
+        self._extend_nullable_fields()
+
     def _get_form_field(self, customfield):
     def _get_form_field(self, customfield):
         return customfield.to_form_field(set_initial=False, enforce_required=False)
         return customfield.to_form_field(set_initial=False, enforce_required=False)
 
 
-    def _append_customfield_fields(self):
-        """
-        Append form fields for all CustomFields assigned to this object type.
-        """
-        nullable_custom_fields = []
-        for customfield in self._get_custom_fields(self._get_content_type()):
-            field_name = f'cf_{customfield.name}'
-            self.fields[field_name] = self._get_form_field(customfield)
-
-            # Record non-required custom fields as nullable
-            if not customfield.required:
-                nullable_custom_fields.append(field_name)
-
-            # Annotate the field in the list of CustomField form fields
-            self.custom_fields[field_name] = customfield
-
-        # Annotate nullable custom fields (if any) on the form instance
-        if nullable_custom_fields:
-            self.nullable_fields = (*self.nullable_fields, *nullable_custom_fields)
+    def _extend_nullable_fields(self):
+        nullable_custom_fields = [
+            name for name, customfield in self.custom_fields.items() if not customfield.required
+        ]
+        self.nullable_fields = (*self.nullable_fields, *nullable_custom_fields)
 
 
 
 
 class NetBoxModelFilterSetForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
 class NetBoxModelFilterSetForm(BootstrapMixin, CustomFieldsMixin, forms.Form):

+ 1 - 1
netbox/templates/inc/panels/custom_fields.html

@@ -7,7 +7,7 @@
       <div class="card-body">
       <div class="card-body">
         {% for group_name, fields in custom_fields.items %}
         {% for group_name, fields in custom_fields.items %}
           {% if group_name %}
           {% if group_name %}
-            <h6><strong>{{ group_name }}</strong></h6>
+            <h6>{{ group_name }}</h6>
           {% endif %}
           {% endif %}
           <table class="table table-hover attr-table">
           <table class="table table-hover attr-table">
             {% for field, value in fields.items %}
             {% for field, value in fields.items %}

+ 9 - 4
netbox/utilities/templates/form_helpers/render_custom_fields.html

@@ -1,7 +1,12 @@
 {% load form_helpers %}
 {% load form_helpers %}
 
 
-{% for field in form %}
-    {% if field.name in form.custom_fields %}
-        {% render_field field %}
-    {% endif %}
+{% for group, fields in form.custom_field_groups.items %}
+  {% if group %}
+  <div class="row">
+    <h6 class="offset-sm-3 mb-3">{{ group }}</h6>
+  </div>
+  {% endif %}
+  {% for name in fields %}
+    {% render_field form|getfield:name %}
+  {% endfor %}
 {% endfor %}
 {% endfor %}