Custom Scripts | field_order setting has no effect #5951

Closed
opened 2025-12-29 19:34:49 +01:00 by adam · 1 comment
Owner

Originally created by @dnikoliouk-dn on GitHub (Jan 17, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.1.4

Python version

3.9

Steps to Reproduce

Configure a custom script as follows:

from django.utils.text import slugify

from dcim.choices import DeviceStatusChoices, SiteStatusChoices
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
from extras.scripts import *


class NewBranchScript(Script):

    class Meta:
        name = "New Branch"
        description = "Provision a new branch site"
        field_order = ['switch_model', 'manufacturer', 'switch_count', 'site_name']

    site_name = StringVar(
        description="Name of the new site"
    )
    switch_count = IntegerVar(
        description="Number of access switches to create"
    )
    manufacturer = ObjectVar(
        model=Manufacturer,
        required=False
    )
    switch_model = ObjectVar(
        description="Access switch model",
        model=DeviceType,
        query_params={
            'manufacturer_id': '$manufacturer'
        }
    )

    def run(self, data, commit):

        # Create the new site
        site = Site(
            name=data['site_name'],
            slug=slugify(data['site_name']),
            status=SiteStatusChoices.STATUS_PLANNED
        )
        site.save()
        self.log_success(f"Created new site: {site}")

        # Create access switches
        switch_role = DeviceRole.objects.get(name='Access Switch')
        for i in range(1, data['switch_count'] + 1):
            switch = Device(
                device_type=data['switch_model'],
                name=f'{site.slug}-switch{i}',
                site=site,
                status=DeviceStatusChoices.STATUS_PLANNED,
                device_role=switch_role
            )
            switch.save()
            self.log_success(f"Created new switch: {switch}")

        # Generate a CSV table of new devices
        output = [
            'name,make,model'
        ]
        for switch in Device.objects.filter(site=site):
            attrs = [
                switch.name,
                switch.device_type.manufacturer.name,
                switch.device_type.model
            ]
            output.append(','.join(attrs))

        return '\n'.join(output)

Expected Behavior

Expected to see fields in the following order:

  1. switch_model
  2. manufacturer
  3. switch_count
  4. site_name

Observed Behavior

The fields are displayed in order they are written in the script.

Originally created by @dnikoliouk-dn on GitHub (Jan 17, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.1.4 ### Python version 3.9 ### Steps to Reproduce Configure a custom script as follows: ``` from django.utils.text import slugify from dcim.choices import DeviceStatusChoices, SiteStatusChoices from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site from extras.scripts import * class NewBranchScript(Script): class Meta: name = "New Branch" description = "Provision a new branch site" field_order = ['switch_model', 'manufacturer', 'switch_count', 'site_name'] site_name = StringVar( description="Name of the new site" ) switch_count = IntegerVar( description="Number of access switches to create" ) manufacturer = ObjectVar( model=Manufacturer, required=False ) switch_model = ObjectVar( description="Access switch model", model=DeviceType, query_params={ 'manufacturer_id': '$manufacturer' } ) def run(self, data, commit): # Create the new site site = Site( name=data['site_name'], slug=slugify(data['site_name']), status=SiteStatusChoices.STATUS_PLANNED ) site.save() self.log_success(f"Created new site: {site}") # Create access switches switch_role = DeviceRole.objects.get(name='Access Switch') for i in range(1, data['switch_count'] + 1): switch = Device( device_type=data['switch_model'], name=f'{site.slug}-switch{i}', site=site, status=DeviceStatusChoices.STATUS_PLANNED, device_role=switch_role ) switch.save() self.log_success(f"Created new switch: {switch}") # Generate a CSV table of new devices output = [ 'name,make,model' ] for switch in Device.objects.filter(site=site): attrs = [ switch.name, switch.device_type.manufacturer.name, switch.device_type.model ] output.append(','.join(attrs)) return '\n'.join(output) ``` ### Expected Behavior Expected to see fields in the following order: 1. switch_model 2. manufacturer 3. switch_count 4. site_name ### Observed Behavior The fields are displayed in order they are written in the script.
adam added the type: featurestatus: needs owner labels 2025-12-29 19:34:49 +01:00
adam closed this issue 2025-12-29 19:34:49 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 18, 2022):

I've reclassified this as a feature request just because the documentation doesn't actually claim to support field ordering (despite the errant example). It'll be easy to add though.

@jeremystretch commented on GitHub (Jan 18, 2022): I've reclassified this as a feature request just because the documentation doesn't actually claim to support field ordering (despite the errant example). It'll be easy to add though.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5951