False Boolean fields reset to True (or default) when using 'Create and Add Another' or cloning. #4744

Closed
opened 2025-12-29 19:20:04 +01:00 by adam · 0 comments
Owner

Originally created by @KayOS65 on GitHub (Apr 9, 2021).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v2.10.8

Python version

3.8

Steps to Reproduce

  1. Add a new 'Device Type'
  2. Enter required field data and UN-CHECK 'is full depth' (is_full_depth == False)
  3. Click 'Create and Add Another'.
  4. Page re-loads with some fields pre-populated. 'Is full depth' is CHECKED (is_full_depth == True)

or

  1. Create a new Device Type with Is full depth == False.
  2. Clone the new Device Type.

Expected Behavior

The 'Is full depth' field is UN-CHECKED when the page re-loads to add another Device Type.
The False state of a Boolean field is maintained when cloning.

Observed Behavior

The state of Boolean fields that are set to False are not carried over when creating an object and then using the 'Create and Add Another' option.

For example, the DeviceType model defines:

clone_fields = [
    'manufacturer', 'u_height', 'is_full_depth', 'subdevice_role',
]

So the Is full depth field value should be carried over when cloning an existing object or when the form re-loads after clicking 'Create and Add Another'. In both cases, the 'is_full_depth' field is omitted from the query string. For example, my test resulted in:

dcim/device-types/add/?manufacturer=1&u_height=1&subdevice_role=parent

The issue is partly in utilities.utils.prepare_cloned_fields(). This function has code that sets any field with a value of False to the empty string (''). The function then only adds fields that aren't set to a value of None or the empty string to the query params.

227        # Swap out False with URL-friendly value
228        if field_value is False:
229            field_value = ''
230
231        # Omit empty values
232        if field_value not in (None, ''):
233            params.append((field_name, field_value)

I've tested commenting out lines 228 and 229. This adds the False boolean field back in to the query params, but still leaves the check-box on the form selected. I'm not sure of a solution to this, whilst still retaining the check-box widget, but I've overcome it by using something like this for the form field:

    is_full_depth = forms.ChoiceField(
        choices=((True, "Yes"), (False, "No"),),
        widget=forms.Select(),
        required=True,
    )
Originally created by @KayOS65 on GitHub (Apr 9, 2021). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v2.10.8 ### Python version 3.8 ### Steps to Reproduce 1. Add a new 'Device Type' 2. Enter required field data and UN-CHECK 'is full depth' (is_full_depth == False) 3. Click 'Create and Add Another'. 4. Page re-loads with some fields pre-populated. 'Is full depth' is CHECKED (is_full_depth == True) or 1. Create a new Device Type with Is full depth == False. 2. Clone the new Device Type. ### Expected Behavior The 'Is full depth' field is UN-CHECKED when the page re-loads to add another Device Type. The False state of a Boolean field is maintained when cloning. ### Observed Behavior The state of Boolean fields that are set to `False` are not carried over when creating an object and then using the 'Create and Add Another' option. For example, the DeviceType model defines: ``` clone_fields = [ 'manufacturer', 'u_height', 'is_full_depth', 'subdevice_role', ] ``` So the Is full depth field value should be carried over when cloning an existing object or when the form re-loads after clicking 'Create and Add Another'. In both cases, the 'is_full_depth' field is omitted from the query string. For example, my test resulted in: `dcim/device-types/add/?manufacturer=1&u_height=1&subdevice_role=parent` The issue is partly in `utilities.utils.prepare_cloned_fields()`. This function has code that sets any field with a value of False to the empty string (''). The function then only adds fields that aren't set to a value of None or the empty string to the query params. ``` 227 # Swap out False with URL-friendly value 228 if field_value is False: 229 field_value = '' 230 231 # Omit empty values 232 if field_value not in (None, ''): 233 params.append((field_name, field_value) ``` I've tested commenting out lines 228 and 229. This adds the False boolean field back in to the query params, but still leaves the check-box on the form selected. I'm not sure of a solution to this, whilst still retaining the check-box widget, but I've overcome it by using something like this for the form field: ``` is_full_depth = forms.ChoiceField( choices=((True, "Yes"), (False, "No"),), widget=forms.Select(), required=True, ) ```
adam added the type: bugstatus: accepted labels 2025-12-29 19:20:04 +01:00
adam closed this issue 2025-12-29 19:20:04 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4744