I have a Django admin form. And now I want to fill it\'s initial field with data based on my model. So I tried this:
class OrderForm(forms.ModelForm):
cl
Since Django 1.7 there is a function get_changeform_initial_data in ModelAdmin that sets initial form values:
def get_changeform_initial_data(self, request):
return {'name': 'custom_initial_value'}
EDIT: Apart from that, @Paul Kenjora's answer applies anyway, which might be useful if you already override get_form
.
In case of inline (InlineModelAdmin
) there is no get_changeform_initial_data
. You can override get_formset
and set formset.form.base_fields['my_field_name'].initial
.