Custom fields with default values are not set when creating a device via a script #4143

Closed
opened 2025-12-29 18:33:25 +01:00 by adam · 1 comment
Owner

Originally created by @991jo on GitHub (Sep 25, 2020).

Environment

  • Python version: 3.6.8
  • NetBox version: 2.9.4

When adding a device via a script the default values for custom fields are not set. The same fields/default values work via the web UI.

Steps to Reproduce

  1. Create a custom field for a device with a default value (see the attached screenshot)
    Screenshot_2020-09-25 Change custom field NetBox(1)
  2. execute a script that creates a device, e.g. the following:
from extras.scripts import Script
from dcim.models import Site, DeviceType, Device, DeviceRole

class AddDeviceDebug(Script):

    class Meta():
        name = "Add device debug"

    def run(self, data, commit):
        device = Device(name="testhostname",
                        device_type=DeviceType.objects.get(model="cisco-dummy-router"),
                        device_role=DeviceRole.objects.get(name="Router"),
                        site=Site.objects.get(name="dummy"))

        device.save()

(the device_type, device_role and site have to be created too)

Expected Behavior

The custom field is set to the default value

Observed Behavior

The custom field is set to "Not defined". If the field is edited the first possible value (for a multiple choice field) is selected by default, not the default value. after saving this first value is set.

Originally created by @991jo on GitHub (Sep 25, 2020). ### Environment * Python version: 3.6.8 * NetBox version: 2.9.4 When adding a device via a script the default values for custom fields are not set. The same fields/default values work via the web UI. ### Steps to Reproduce 1. Create a custom field for a device with a default value (see the attached screenshot) ![Screenshot_2020-09-25 Change custom field NetBox(1)](https://user-images.githubusercontent.com/1368323/94255429-88602b00-ff28-11ea-8fcf-1bdbfacde62b.png) 2. execute a script that creates a device, e.g. the following: ``` from extras.scripts import Script from dcim.models import Site, DeviceType, Device, DeviceRole class AddDeviceDebug(Script): class Meta(): name = "Add device debug" def run(self, data, commit): device = Device(name="testhostname", device_type=DeviceType.objects.get(model="cisco-dummy-router"), device_role=DeviceRole.objects.get(name="Router"), site=Site.objects.get(name="dummy")) device.save() ``` (the device_type, device_role and site have to be created too) ### Expected Behavior The custom field is set to the default value <!-- What happened instead? --> ### Observed Behavior The custom field is set to "Not defined". If the field is edited the first possible value (for a multiple choice field) is selected by default, not the default value. after saving this first value is set.
adam closed this issue 2025-12-29 18:33:25 +01:00
Author
Owner

@jeremystretch commented on GitHub (Sep 25, 2020):

Custom field values are not assigned automatically on the model. As the author of the script, you need to assign them directly as they are by other components in NetBox. I realize this isn't convenient, but custom fields were implemented long before custom scripts were supported, so user-friendliness wasn't a design goal. FYI, the entire custom fields data model has been reimplemented for the upcoming v2.10 release (see #4878).

@jeremystretch commented on GitHub (Sep 25, 2020): Custom field values are not assigned automatically on the model. As the author of the script, you need to assign them directly as they are by other components in NetBox. I realize this isn't convenient, but custom fields were implemented long before custom scripts were supported, so user-friendliness wasn't a design goal. FYI, the entire custom fields data model has been reimplemented for the upcoming v2.10 release (see #4878).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4143