It's for beginners to some extent who have used all the basic features of Django.
This is an explanation of the initial value settings that you feel addicted to when using the form. Note that the description may not clearly distinguish between Form and ModelForm.
First, let's illustrate a rough image of the form.
Next, it represents the main input items of the form (items of the argument of the \ _ \ _ init \ _ \ _ () method).
The following shows what each input item corresponds to.
There is a description in the official document that "the distinction between Bound / Unbound is important", but it is difficult to understand the behavior of the form unless you hold down this. The definition of Bound / Unbound is as follows.
This can also be seen from the source code of the BaseForm class.
#Part of the code for the BaseForm class
self.is_bound = data is not None or files is not None
Here are some examples of what arguments can be used to instantiate a form to make it Bound (or Unbound).
# Bound/Unbound form example
form_bound_1 = SomeForm(request.GET)
form_bound_2 = SomeForm(request.POST)
form_bound_3 = SomeForm(request.POST, request.FILES)
form_unbound_1 = SomeForm()
form_unbound_2 = SomeForm(initial={"some_field": "some_valule"})
form_unbound_3 = SomeModelForm(initial={"some_field": "some_valule"}, instance=some_instance)
form_bound_4 = SomeModelForm(request.POST, initial={"some_field": "some_valule"}, instance=some_instance)
The "main input items of the form" and "Bound / Unbound" that we have seen so far are related to the initial value setting. The form defines one or more input fields, or fields, but let's look at the code where each field gets a value to render the Html markup.
#BoundField class methods
def value(self):
"""
Return the value for this BoundField, using the initial value if
the form is not bound or the data otherwise.
"""
data = self.initial
if self.form.is_bound:
data = self.field.bound_data(self.data, data)
return self.field.prepare_value(data)
From the above, we can see that Bound form field values do not use (overwrite) initial </ b>. Also, note that even if request.GET etc. is empty (≠ None), it will be a Bound form </ b>.
If you want to create a search form or use Ajax to do this on one screen, you can enable the initial value setting by branching as follows. (The code is just an example.)
if "some_field" in request.GET:
form = SomeForm(request.GET)
else:
form = SomeForm(initial={"some_field": "some_value"})
The problem does not occur while model creation and model update are executed in individual views, but it may become addictive once you start applying it.
In addition, it should be noted that the values of the form fields are set in the following priority order. (To be precise, it's like a "priority image" that ignores Bound / Unbound differences, etc.)
In ModelForm, the default value of Model seems to be the initial of the form field, but it doesn't seem to be.
The "Bound / Unbound" and "Initial value setting" parts are parts that you can't imagine, so it's a good idea to deepen your understanding.
Recommended Posts