Description
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