Skip to content

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

Closed
@jeremystretch

Description

@jeremystretch

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

Metadata

Metadata

Assignees

Labels

severity: mediumResults in substantial degraded or broken functionality for specfic workflowsstatus: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions