Skip to content
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

get_field_value() does not recognize null field values on bound forms #19023

Closed
jeremystretch opened this issue Mar 27, 2025 · 2 comments · Fixed by #19024
Closed

get_field_value() does not recognize null field values on bound forms #19023

jeremystretch opened this issue Mar 27, 2025 · 2 comments · Fixed by #19024
Assignees
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Milestone

Comments

@jeremystretch
Copy link
Member

Deployment Type

Self-hosted

NetBox Version

v4.2.6

Python Version

3.10

Steps to Reproduce

This was uncovered while using the get_field_value() utility function for dynamic form rendering. A simple proof is provided below.

from django import forms
from dcim.models import Site
from utilities.forms.utils import get_field_value

class MyForm(forms.Form):
    site = forms.ModelChoiceField(
        queryset=Site.objects.all(),
        required=False
    )

form = MyForm({'site': None}, initial={'site': 1})
get_field_value(form, 'site')

Expected Behavior

The function should always return the value of the field in a bound form.

Observed Behavior

When a null value is specified on a bound form, the function defers to the field's initial value:

>>> form = MyForm({'site': 2}, initial={'site': 1})
>>> get_field_value(form, 'site')
2
>>> form = MyForm({'site': None}, initial={'site': 1})
>>> get_field_value(form, 'site')
1
@jeremystretch jeremystretch added severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application labels Mar 27, 2025
@jeremystretch jeremystretch self-assigned this Mar 27, 2025
@pheus
Copy link
Contributor

pheus commented Mar 27, 2025

Just a quick thought: Could this be a case where plugins might rely on this behavior? If so, perhaps it would be safer to target the fix for v4.3 to minimize any potential disruptions.

@jeremystretch
Copy link
Member Author

Bug fixes always go in patch releases, as the intent is to correct unexpected behavior.

@jeremystretch jeremystretch added this to the v4.2.7 milestone Apr 1, 2025
jnovinger pushed a commit that referenced this issue Apr 1, 2025
* Move Module & ModuleType models to a separate file

* Add ModuleTypeProfile & related fields

* Initial work on JSON schema validation

* Add attributes property on ModuleType

* Introduce MultipleOfValidator

* Introduce JSONSchemaProperty

* Enable dynamic form field rendering

* Misc cleanup

* Fix migration conflict

* Ensure deterministic ordering of attriubte fields

* Support choices & default values

* Include module type attributes on module view

* Enable modifying individual attributes via REST API

* Enable filtering by attribute values

* Add documentation & tests

* Schema should be optional

* Include attributes column for profiles

* Profile is nullable

* Include some initial profiles to be installed via migration

* Fix migrations conflict

* Fix filterset test

* Misc cleanup

* Fixes #19023: get_field_value() should respect null values in bound forms (#19024)

* Skip filters which do not specify a JSON-serializable value

* Fix handling of array item types

* Fix initial data in schema field during bulk edit

* Implement sanity checking for JSON schema definitions

* Fall back to filtering by string value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants