Does anyone know how to prepopulate a form with model data

9 views
Skip to first unread message

codecowboy

unread,
Apr 8, 2009, 9:55:15 PM4/8/09
to Django users
I want to create a form that allows a user to edit his/her profile. I
don't want to bind my form to a model because the form will involve
two different models. Does anyone have any ideas. I've read chapter
7 in the Django Book but it only to a simple example that does not
help me.

Here is what I have tried.

s = get_object_or_404(Scientist, pk=7)
form = ScientistProfileForm(initial=s)

I've also tried.

form = ScientistProfileForm(s)

I always get the following error message.

Caught an exception while rendering: 'Scientist' object has no
attribute 'get'

Thanks in advance for any help. If I figure this out then I will post
my solution in here in great detail for anyone else that needs it.

Briel

unread,
Apr 9, 2009, 2:18:50 AM4/9/09
to Django users
Hi.
It seems like your error and your problem is not the same. The error
is complaining about the get_object_or_404, not the profile thing. So
changing that code won't actually do anything as it doesn't get that
far.

~Jakob

Adi Sieker

unread,
Apr 9, 2009, 3:25:35 AM4/9/09
to django...@googlegroups.com

On 09.04.2009, at 08:18, Briel wrote:

>
> Hi.
> It seems like your error and your problem is not the same. The error
> is complaining about the get_object_or_404, not the profile thing. So
> changing that code won't actually do anything as it doesn't get that
> far.
>
Well, maybe not. I had the same error yesterday.
If you pass a model instance as the value dict, then you get the
exceptions he mentioned.

> ~Jakob
>
> On Apr 9, 3:55 am, codecowboy <guy.ja...@gmail.com> wrote:
>> I want to create a form that allows a user to edit his/her
>> profile. I
>> don't want to bind my form to a model because the form will involve
>> two different models. Does anyone have any ideas. I've read chapter
>> 7 in the Django Book but it only to a simple example that does not
>> help me.
>>
>> Here is what I have tried.
>>
>> s = get_object_or_404(Scientist, pk=7)
>> form = ScientistProfileForm(initial=s)
>>

have you tried making a dict with the keys being the fieldnames and
the dict values should then be prepopulated in the fields.

adi

Daniel Roseman

unread,
Apr 9, 2009, 4:02:41 AM4/9/09
to Django users
If it's not a ModelForm, you can't just pass in an instance. You'll
need a dictionary.

initial_dict = {
'name': s.name,
... etc ...
}
form = ScientistProfileForm(initial=initial_dict)

--
DR.

Milan Andric

unread,
Apr 28, 2009, 8:36:52 PM4/28/09
to Django users


On Apr 9, 1:02 am, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
> On Apr 9, 2:55 am, codecowboy <guy.ja...@gmail.com> wrote:
>
>
>
> > I want to create aformthat allows a user to edit his/her profile.  I
> > don't want to bind myformto a model because theformwill involve
> > two different models.  Does anyone have any ideas.  I've read chapter
> > 7 in the Django Book but it only to a simple example that does not
> > help me.
>
> > Here is what I have tried.
>
> > s = get_object_or_404(Scientist, pk=7)
> >form= ScientistProfileForm(initial=s)
>
> > I've also tried.
>
> >form= ScientistProfileForm(s)
>
> > I always get the following error message.
>
> > Caught an exception while rendering: 'Scientist' object has no
> > attribute 'get'
>
> > Thanks in advance for any help.  If I figure this out then I will post
> > my solution in here in great detail for anyone else that needs it.
>
> If it's not a ModelForm, you can't just pass in an instance. You'll
> need a dictionary.
>
> initial_dict = {
>     'name': s.name,
>     ... etc ...}
>
> form= ScientistProfileForm(initial=initial_dict)
>

Anything wrong with doing this? form = ScientistProfileForm( initial
= s.__dict__ )

--
Milan
Reply all
Reply to author
Forward
0 new messages