You're actually looking for the "initial" parameter of the form
initialization function.  This is a dict you can pass to the form to
set the "initial" data.

so in your example you would want to pass to the form something like

m = MyModel.objects.get(pk=1)
f = MyForm(initial = {'field1': m.field1, "field2': m.field2,})

supposing you don't have an relations in the model you can simple pass
it the __dict__ attribute of your model instance.

f = MyForm(initial = m.__dict__)

this works pretty well and simply needs to have the ManyToMany and
ForeignKey fields set up individually.  However that's a bit of a pain
to be doing over and over, and if a model has a signifigant number of
relations it's not particularly useful.  What I've been looking at
trying to pull together is a function that converts a model to a dict
that has the relations in forms that the form initialization function
understands.

~ Anders

On 3/6/07, orestis <[EMAIL PROTECTED]> wrote:
>
> Since form_for_model isn't nearly finished yet (regarding with
> FileFields and customization - or I just can't find how it works), I
> have decided to just repeat myself and create my own forms.
>
> I thought that it'd be very intuitive if a form instance gets bound to
> the data from a model instance, but it seems it doesn't. To clarify
> things:
>
> def MyForm(newforms.Form):
>   field1=newforms.CharField()
>   field2=newforms.CharField()
>
>
> def MyModel(models.Model):
>   field1=models.TextField()
>   field2=models.TextField()
>
>
> m = MyModel.objects.get(pk=1)
> f = MyForm(f) # this should pre-populate the form with the data from
> the model, but it doesn't.
> #In fact, nothing even gets displayed on the screen...
>
> Am I assuming something wrong ? I'd love to use form_for_model, but
> I'm using compound models (it's a user profile, really) and it's not
> very useful, as I'll still have to customize it extensively.
>
> Or should I just display more than one form on a page ? Meaning:
>
> def myview(request):
>   form1 = MyForm1(request.POST) # some fields are here
>   form2 = MyForm2(request.POST) # some other fields are here
>   #save the forms.
>
> The user will see just one form, but it'll be split behind the scenes
> to map to different Forms and Models...
>
> Do I make sense ?
>
> Thanks :)
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to