Skip to content

Extend fieldsets to support rendering complex form layouts #14739

Closed
@jeremystretch

Description

@jeremystretch

NetBox version

v3.7.0

Feature type

New functionality

Proposed functionality

Introduce several new classes that would enable more complex form rendering via the fieldsets attribute on a form. For example, the following is defined on SiteForm.Meta:

fieldsets = (
    (_('Site'), (
        'name', 'slug', 'status', 'region', 'group', 'facility', 'asns', 'time_zone', 'description',
        'tags',
    )),
    (_('Tenancy'), ('tenant_group', 'tenant')),
    (_('Contact Info'), ('physical_address', 'shipping_address', 'latitude', 'longitude')),
)

This causes the form to render three discrete groups of fields with the assigned headings:

site_form

This proposal seeks to extend this ability by enabling several forms of more complex rendering, detailed below.

Static Attribute Rendering

Display an object attribute (not necessarily a form field) of an existing object, rendered as a read-only field.

fieldsets = (
    (None, (
        ObjectAttribute('parent'), 'name', 'image',
    )),
)

image_attachment

Side-by-Side Fields

Display several fields inline on a single row within the form.

fieldsets = (
    (_('Dimensions'), (
        'type', 'width', 'u_height', InlineFields('outer_width', 'outer_depth', 'outer_unit'),
    )),
)

rack_dimensions

Tabbed Groups

Allow fields to be arranged in tabbed subgroups. Selecting the tab for a group will display its fields and hide fields from its peers.

fieldsets = (
    (
        _('Interface Assignment'),
        TabbedFieldGroups(
            (_('Device'), 'interface'),
            (_('VirtualMachine'), 'vminterface'),
            (_('FHRP Group'), 'fhrpgroup'),
        ), 'primary_for_parent,
    ),
)

ipaddress_assignment

Use case

There are several instances across NetBox where we use custom HTML templates to render object edit forms, because we need to deviate from the standard form rendering and accommodate a more complex layout. Some instances include:

  • Displaying an attribute as a read-only field (e.g. the object to which an image attachment is assigned)
  • Grouping several fields under tabs (e.g. IP address interface/FHRP group)
  • Displaying several fields inline (e.g. rack width, depth, and unit)

Moving this logic into the form fieldset definitions will allow us to ditch most of our custom templates.

Database changes

No response

External dependencies

No response

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: featureIntroduction of new functionality to the application

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions