Django Gotchas #1 - Dynamic Initial Values In Forms!


Django form fields accept initial argument. So You can set a  default value for  a field.

Sometimes it is required to override __init__ method  in forms and set field initial arguments.

Now let's pass some initial data to form and see what happens.
If You look at the value of input field, it's is NOT the overrided. It still has form initial value! If You look into source code of django forms to find what is happening, You will find this.
So form's initial value has precedence over fields initial values.

So You have to override form's initial value instead of fields's initial value to make it work as expected.

Read official docs about django forms.

Read more articles about Python!