mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-19 03:42:25 -06:00
Introduce ObjectAttribute for displaying read-only instance attributes on forms
This commit is contained in:
@@ -3,8 +3,8 @@ import string
|
||||
from functools import cached_property
|
||||
|
||||
__all__ = (
|
||||
'FieldGroup',
|
||||
'InlineFields',
|
||||
'ObjectAttribute',
|
||||
'TabbedFieldGroups',
|
||||
)
|
||||
|
||||
@@ -41,3 +41,9 @@ class TabbedFieldGroups:
|
||||
'fields': group.field_names,
|
||||
} for i, group in enumerate(self.groups, start=1)
|
||||
]
|
||||
|
||||
|
||||
class ObjectAttribute:
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
@@ -12,6 +12,17 @@
|
||||
{# Single form field #}
|
||||
{% render_field items.0 %}
|
||||
|
||||
{% elif layout == 'attribute' %}
|
||||
{# A static attribute of the form's instance #}
|
||||
<div class="row mb-3">
|
||||
<label class="col-sm-3 col-form-label text-lg-end required">{{ title }}</label>
|
||||
<div class="col">
|
||||
<div class="form-control-plaintext">
|
||||
{{ items.0|linkify }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% elif layout == 'inline' %}
|
||||
{# Multiple form fields on the same line #}
|
||||
<div class="row mb-3">
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from django import template
|
||||
|
||||
from utilities.forms.rendering import InlineFields, TabbedFieldGroups
|
||||
from utilities.forms.rendering import InlineFields, ObjectAttribute, TabbedFieldGroups
|
||||
|
||||
__all__ = (
|
||||
'getfield',
|
||||
@@ -81,6 +81,13 @@ def render_fieldset(form, fieldset, heading=None):
|
||||
('tabs', None, tabs)
|
||||
)
|
||||
|
||||
elif type(item) is ObjectAttribute:
|
||||
value = getattr(form.instance, item.name)
|
||||
label = value._meta.verbose_name if hasattr(value, '_meta') else item.name
|
||||
rows.append(
|
||||
('attribute', label.title(), [value])
|
||||
)
|
||||
|
||||
# A single form field
|
||||
elif item in form.fields:
|
||||
rows.append(
|
||||
|
||||
Reference in New Issue
Block a user