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

This commit is contained in:
jeremystretch 2022-07-26 12:41:51 -04:00
parent d442f8fd60
commit 466931d2fb
5 changed files with 23 additions and 24 deletions

View File

@ -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
* [#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
* [#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)
* [#9844](https://github.com/netbox-community/netbox/issues/9844) - Fix interface api request when creating/editing L2VPN termination

View File

@ -19,6 +19,7 @@ class CustomFieldsMixin:
def __init__(self, *args, **kwargs):
self.custom_fields = {}
self.custom_field_groups = {}
super().__init__(*args, **kwargs)
@ -58,3 +59,6 @@ class CustomFieldsMixin:
# Annotate the field in the list of CustomField form fields
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)

View File

@ -94,29 +94,18 @@ class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['pk'].queryset = self.model.objects.all()
self._extend_nullable_fields()
def _get_form_field(self, customfield):
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:
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)

View File

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

View File

@ -1,7 +1,12 @@
{% load form_helpers %}
{% for field in form %}
{% if field.name in form.custom_fields %}
{% render_field field %}
{% 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 %}