Skip to content

Commit 446240d

Browse files
authored
Merge pull request #121 from netboxlabs/35-group-name
35 fix group name on edit form
2 parents 1d54924 + 1c32d0e commit 446240d

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends 'generic/object_edit.html' %}
2+
{% load form_helpers %}
23
{% load i18n %}
34

45
{% block title %}
@@ -9,4 +10,25 @@
910
Add a new {{ custom_object_type }}
1011
{% endblocktrans %}
1112
{% endif %}
12-
{% endblock title %}
13+
{% endblock title %}
14+
15+
{% block form %}
16+
{# Render hidden fields #}
17+
{% for field in form.hidden_fields %}
18+
{{ field }}
19+
{% endfor %}
20+
21+
{# Render all fields grouped by group_name #}
22+
<div class="field-group mb-5">
23+
{% for group, fields in form.custom_field_groups.items %}
24+
{% if group %}
25+
<div class="row">
26+
<h3 class="col-9 offset-3 mb-3 h4">{{ group }}</h3>
27+
</div>
28+
{% endif %}
29+
{% for name in fields %}
30+
{% render_field form|getfield:name %}
31+
{% endfor %}
32+
{% endfor %}
33+
</div>
34+
{% endblock form %}

netbox_custom_objects/views.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,22 +368,44 @@ def get_form(self, model):
368368
"Meta": meta,
369369
"__module__": "database.forms",
370370
"_errors": None,
371+
"custom_fields": {},
372+
"custom_field_groups": {},
371373
}
372374

373-
for field in self.object.custom_object_type.fields.all():
375+
for field in self.object.custom_object_type.fields.all().order_by('group_name', 'weight', 'name'):
374376
field_type = field_types.FIELD_TYPE_CLASS[field.type]()
375377
try:
376-
attrs[field.name] = field_type.get_annotated_form_field(field)
378+
field_name = field.name
379+
attrs[field_name] = field_type.get_annotated_form_field(field)
380+
381+
# Annotate the field in the list of CustomField form fields
382+
attrs["custom_fields"][field_name] = field
383+
384+
# Group fields by group_name (similar to NetBox custom fields)
385+
group_name = field.group_name or None # Use None for ungrouped fields
386+
if group_name not in attrs["custom_field_groups"]:
387+
attrs["custom_field_groups"][group_name] = []
388+
attrs["custom_field_groups"][group_name].append(field_name)
389+
377390
except NotImplementedError:
378391
print(f"get_form: {field.name} field is not supported")
379392

380-
form = type(
393+
# Create a custom __init__ method to set instance attributes
394+
def custom_init(self, *args, **kwargs):
395+
super(form_class, self).__init__(*args, **kwargs)
396+
# Set the grouping info as instance attributes from the outer scope
397+
self.custom_fields = attrs["custom_fields"]
398+
self.custom_field_groups = attrs["custom_field_groups"]
399+
400+
attrs["__init__"] = custom_init
401+
402+
form_class = type(
381403
f"{model._meta.object_name}Form",
382404
(forms.NetBoxModelForm,),
383405
attrs,
384406
)
385407

386-
return form
408+
return form_class
387409

388410

389411
@register_model_view(CustomObject, "delete")

0 commit comments

Comments
 (0)