I'm using Django 2.2 forms and I'm trying to understand why the below two snippets have different behaviors. Why does passing a @staticmethod to initial
in the form field (snippet A) not have the same result as passing an unbound function (snippet B)?
Snippet A:
class BankUpdateForm(forms.Form):
@staticmethod
def yesterday():
return date.today() - timedelta(days=1)
from_date = forms.DateField(initial=yesterday)
to_date = forms.DateField(initial=date.today)
Snippet B:
def yesterday():
return date.today() - timedelta(days=1)
class BankUpdateForm(forms.Form):
from_date = forms.DateField(initial=yesterday)
to_date = forms.DateField(initial=date.today)
Snippet B will work as intended and show the correct initial field value. Snippet A will only print the function's str
.