﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
5126	Populate `initial_data` of forms from model instance	Christopher Lenz <cmlenz@…>	nobody	"Okay, I've looked for ways to populate the initial data of a form created by `newforms.form_for_model()` from an existing model instance.

I can't use `form_for_instance` because I want to add custom `clean_` hooks, so the pattern I'm using looks like this:

{{{
#!python
class MyForm(forms.form_for_model(MyModel, fields=['foo', 'bar'])):
    def clean_foo(self):
        # ...
}}}

Now when I create a form instance, I can't see a clean way to populate the inital data of the form properly. I've tried the following, but it doesn't work for relations, plus it's really ugly:

{{{
#!python
  form = MyForm(initial=myinstance.__dict__)
}}}

The only option seems to be overriding the `__init__()`, but that's a pain as I'd have to do that for every form. I guess I could probably work around this somehow, but I can't believe this isn't a problem others are seeing, so I'm attaching a patch that overrides the default `__init__()` to take the instance as first (optional) argument, which makes a lot of sense to me. The code would then look like this:

{{{
#!python
  form = MyForm(myinstance)
}}}

Kindly asking for review/comments. Thanks."		closed	Forms	dev		invalid	feature	akaihol+django@…	Design decision needed	1	0	1	0	0	0
