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

Closed
opened 2025-12-29 21:38:22 +01:00 by adam · 2 comments
Owner

Originally created by @jeremystretch on GitHub (Mar 27, 2025).

Originally assigned to: @jeremystretch on GitHub.

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
Originally created by @jeremystretch on GitHub (Mar 27, 2025). Originally assigned to: @jeremystretch on GitHub. ### 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()`](https://github.com/netbox-community/netbox/blob/e1e514251e825fadfcc87a191502a931fa9edb5b/netbox/utilities/forms/utils.py#L132) utility function for dynamic form rendering. A simple proof is provided below. ```python 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: ```python >>> 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 ```
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 21:38:22 +01:00
adam closed this issue 2025-12-29 21:38:22 +01:00
Author
Owner

@pheus commented on GitHub (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.

@pheus commented on GitHub (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.
Author
Owner

@jeremystretch commented on GitHub (Mar 27, 2025):

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

@jeremystretch commented on GitHub (Mar 27, 2025): Bug fixes always go in patch releases, as the intent is to correct unexpected behavior.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10955