Custom fields default value on Objects from modules are not set #10387

Closed
opened 2025-12-29 21:30:47 +01:00 by adam · 1 comment
Owner

Originally created by @Etibru on GitHub (Oct 21, 2024).

Originally assigned to: @bctiemann on GitHub.

Deployment Type

Self-hosted

Triage priority

N/A

NetBox Version

v4.1.4

Python Version

3.12

Steps to Reproduce

  1. Create a required custom field and set a default value. Choose an object like DCIM > Interface.
{
    "id": 8,
    "display": "My cf",
    "object_types": [
        "dcim.interface"
    ],
    "type": {
        "value": "text",
        "label": "Text"
    },
    "related_object_type": null,
    "data_type": "string",
    "name": "My_CF",
    "label": "",
    "group_name": "",
    "description": "",
    "required": true,
    "unique": false,
    "search_weight": 1000,
    "filter_logic": {
        "value": "loose",
        "label": "Loose"
    },
    "ui_visible": {
        "value": "always",
        "label": "Always"
    },
    "ui_editable": {
        "value": "yes",
        "label": "Yes"
    },
    "is_cloneable": false,
    "default": "default_value",
    "related_object_filter": null,
    "weight": 100,
    "validation_minimum": null,
    "validation_maximum": null,
    "validation_regex": "",
    "choice_set": null,
    "comments": "",
}

2.Create a module type with a few interfaces.
17295182262847603361889833276334

  1. Create a device, create a module bay on this device.
    17295182378236963683433093075664

  2. Connect a module with the module type
    17295182497146946160325110780536

Now, we have the interfaces from the type module that have been added to the device.
17295182632186578896907287261420

And if we see an Interface:
17295182817146530767661734069473

We can see that the default value has not been set.

"custom_fields": {
"My_CF": null
},
Note that in devices with device_type, interface custom fields (for example) are set to the default value

Expected Behavior

We should have the default value and not null. on the same way as on a device type with interfaces, when the device is created, there will be the default value on the interfaces custom field.

Observed Behavior

Interfaces that are created when the module is connected to a device, the default values of custom fields are not set to their default values.

Originally created by @Etibru on GitHub (Oct 21, 2024). Originally assigned to: @bctiemann on GitHub. ### Deployment Type Self-hosted ### Triage priority N/A ### NetBox Version v4.1.4 ### Python Version 3.12 ### Steps to Reproduce 1. Create a required custom field and set a default value. Choose an object like DCIM > Interface. ```json { "id": 8, "display": "My cf", "object_types": [ "dcim.interface" ], "type": { "value": "text", "label": "Text" }, "related_object_type": null, "data_type": "string", "name": "My_CF", "label": "", "group_name": "", "description": "", "required": true, "unique": false, "search_weight": 1000, "filter_logic": { "value": "loose", "label": "Loose" }, "ui_visible": { "value": "always", "label": "Always" }, "ui_editable": { "value": "yes", "label": "Yes" }, "is_cloneable": false, "default": "default_value", "related_object_filter": null, "weight": 100, "validation_minimum": null, "validation_maximum": null, "validation_regex": "", "choice_set": null, "comments": "", } ``` 2.Create a module type with a few interfaces. ![17295182262847603361889833276334](https://github.com/user-attachments/assets/9b7310a4-aeee-4cfc-a5ba-d83510c49bcc) 3. Create a device, create a module bay on this device. ![17295182378236963683433093075664](https://github.com/user-attachments/assets/a8ed41c9-ad28-4ec1-b416-1a0526f9848d) 4. Connect a module with the module type ![17295182497146946160325110780536](https://github.com/user-attachments/assets/e9683de4-54fa-49e7-8ad8-87d9a4986869) Now, we have the interfaces from the type module that have been added to the device. ![17295182632186578896907287261420](https://github.com/user-attachments/assets/feef89ac-e371-4da4-a190-e7d1a6c86389) And if we see an Interface: ![17295182817146530767661734069473](https://github.com/user-attachments/assets/d7d6564d-2210-48e4-ac40-107f6d271e3d) We can see that the default value has not been set. "custom_fields": { "My_CF": null }, Note that in devices with device_type, interface custom fields (for example) are set to the default value ### Expected Behavior We should have the default value and not null. on the same way as on a device type with interfaces, when the device is created, there will be the default value on the interfaces custom field. ### Observed Behavior Interfaces that are created when the module is connected to a device, the default values of custom fields are not set to their default values.
adam added the type: bugstatus: acceptednetboxseverity: medium labels 2025-12-29 21:30:47 +01:00
adam closed this issue 2025-12-29 21:30:47 +01:00
Author
Owner

@Etibru commented on GitHub (Nov 5, 2024):

I've had a quick look at where the SET default value is missing.

HERE, when a device is created and components added, the custom field value is set to default.

               # Set default values for any applicable custom fields
                if cf_defaults := CustomField.objects.get_defaults_for_model(model):
                    component.custom_field_data = cf_defaults
                component.save()

For module bay components, you'll need to add more or less the same resonance HERE.

Something like this :

                for component in create_instances:
                    if cf_defaults := CustomField.objects.get_defaults_for_model(component_model):
                        component.custom_field_data = cf_defaults
                    post_save.send(
                        sender=component_model,
                        instance=component,
                        created=True,
                        raw=False,
                        using='default',
                        update_fields=None
                    )

Maybe there's something better to do

@Etibru commented on GitHub (Nov 5, 2024): I've had a quick look at where the SET default value is missing. [HERE](https://github.com/netbox-community/netbox/blob/ead6e637f4aecc4717b10c71f9140c94040da264/netbox/dcim/models/devices.py#L1019C17-L1020C62), when a device is created and components added, the custom field value is set to default. ```py # Set default values for any applicable custom fields if cf_defaults := CustomField.objects.get_defaults_for_model(model): component.custom_field_data = cf_defaults component.save() ``` For module bay components, you'll need to add more or less the same resonance [HERE](https://github.com/netbox-community/netbox/blob/ead6e637f4aecc4717b10c71f9140c94040da264/netbox/dcim/models/devices.py#L1276). Something like this : ```py for component in create_instances: if cf_defaults := CustomField.objects.get_defaults_for_model(component_model): component.custom_field_data = cf_defaults post_save.send( sender=component_model, instance=component, created=True, raw=False, using='default', update_fields=None ) ``` Maybe there's something better to do
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10387