These are the options that I would checkout:
Option 1: Provide initial form data when instantiating the form.
This is the most basic way to do it. In your views.py, simply provide the data with the initial
keyword argument.
views.py
from forms import ExampleForm
INITIAL_DATA = {'name': 'initial_name'}
def my_view(request):
...
form = ExampleForm(initial=INITIAL_DATA)
...
Not too tricky. The only downside would be if you use and abuse that form and get tired of passing in the initial data.
Option 2 (ideal for your case): Provide the initial data in the __init__
method of the class.
Forms in Django are designed to accept initial data at instantiation, so you can mess with those values in the __init__
method. Without testing it, this what I imagine it would look like:
forms.py
class ExampleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
"""If no initial data, provide some defaults."""
initial = kwargs.get('initial', {})
initial['name'] = 'initial_name'
kwargs['initial'] = initial
super(ExampleForm, self).__init__(*args, **kwargs)
This would be optimal because you can ignore the initial={...}
in the view code.