Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KayOS65 opened this issue Apr 9, 2021 · 0 comments
Assignees
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@KayOS65
Copy link

KayOS65 commented Apr 9, 2021

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,
    )
@KayOS65 KayOS65 added the type: bug A confirmed report of unexpected behavior in the application label Apr 9, 2021
@jeremystretch jeremystretch added the status: accepted This issue has been accepted for implementation label Apr 9, 2021
@jeremystretch jeremystretch self-assigned this Apr 9, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

2 participants