It seems odd to me and caused a little of a headache that BoundField
’s value
method returns different values for GET and POST.
I am using a DateField
and for initial the value is python datetime.date
. However, I am doing asynchronous validation and when the form is initialized using the POST data the value of my date field is a string.
This caused issues because in the template I was passing the field.value
into the date
template filter like so: field.value|date:"Y-m-d"
. The date
filter requires the value to be a date or it returns nothing. The inconsistency was frustrating.
Why not always transform the data using the field’s to_python
method that way it is consistent?
FYI if it makes it easier:
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)