[Django]-Add and initialize custom field in Django ModelForm

7👍

✅

You have to set the initial value. In your init try replace with below code.

self.fields['last_name'].initial = last_name
self.fields['my_field'].initial = my_field

The initial can also be passed when the form instance is created.

form = PersonForm(instance=person, initial={'last_name'='Smith', 'my_field'='test123'})

This one is the recommended way to do it. Don’t have to override the init method at all.

Leave a comment