Re: Using newforms to create multiple related objects?

2007-07-05 Thread Russell Keith-Magee

On 6/29/07, Michael Sylvan <[EMAIL PROTECTED]> wrote:
>
> Is there a way to do this sort of things cleanly, short of using the
> newforms-admin branch of Django? Or do I have to do something like
> changing the form's fields attribute by hand?

Using formsets from newforms-admin will be the easiest way to do this.
However, if you don't want to go down that path, you can use the
existing forms code without hand coding the id attributes.

When you create the form instance, you can specify a prefix that is
used for the name and id for each input on that form. e.g.,:

MyForm = form_for_model(Phone)

form1 = MyForm(prefix='phone1')
form2 = MyForm(prefix='phone2')
..

will generate two form instances; all the field names and id's for
form1 will be prefixed by 'phone1', and so on. This avoids the
inevitable name/id clash from having multiple instances of the same
form on a page.

This is true of _any_ form, whether created using form_for_model,
form_for_instance, manual definition, or anything else.

Yours,
Russ Magee %-)

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Using newforms to create multiple related objects?

2007-07-04 Thread yml

Hello,

It would be great if one of the Gurus could spend some time to answer
this kind of questions. I am trying to do something similar. But so
far I haven't been able to achieve this.
I am looking for a way to build dynamically a form representing a
mashup of several models and its associated view.

Thank you

On Jun 29, 3:45 am, Michael Sylvan <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I am trying to use newforms and templates, together with the following
> models, to mimic what the admin application does with num_in_admin :
> provide a form for Person's fields, and multiple Phone forms.
>
> With just one Phone form things are quite straightforward: validate
> the Person form, and if that passes, save the object, get its ID, fill
> the ID field of the Phone object and then save it.
>
> With multiple Phone forms, however, I have not been able to customize
> the HTML IDs of the various fields -- form_for_model generates
> identical forms!
>
> One can modify form_for_model with a custom formfield_callback
> function, but as far as I can tell, it maps a field name to a field
> object, so it won't be enough to override that.
>
> Is there a way to do this sort of things cleanly, short of using the
> newforms-admin branch of Django? Or do I have to do something like
> changing the form's fields attribute by hand?
>
> Thanks,
>
> -- M. Sylvan
>
> class Person(models.Model):
> firstName = models.CharField(maxlength=32)
> lastName  = models.CharField(maxlength=32)
>
> class Phone(models.Model):
> person = models.ForeignKey(Person)
> number = models.CharField(maxlength=24, core=True)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Using newforms to create multiple related objects?

2007-06-28 Thread Michael Sylvan

Hello,

I am trying to use newforms and templates, together with the following
models, to mimic what the admin application does with num_in_admin :
provide a form for Person's fields, and multiple Phone forms.

With just one Phone form things are quite straightforward: validate
the Person form, and if that passes, save the object, get its ID, fill
the ID field of the Phone object and then save it.

With multiple Phone forms, however, I have not been able to customize
the HTML IDs of the various fields -- form_for_model generates
identical forms!

One can modify form_for_model with a custom formfield_callback
function, but as far as I can tell, it maps a field name to a field
object, so it won't be enough to override that.

Is there a way to do this sort of things cleanly, short of using the
newforms-admin branch of Django? Or do I have to do something like
changing the form's fields attribute by hand?

Thanks,

-- M. Sylvan

class Person(models.Model):
firstName = models.CharField(maxlength=32)
lastName  = models.CharField(maxlength=32)

class Phone(models.Model):
person = models.ForeignKey(Person)
number = models.CharField(maxlength=24, core=True)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---