I agree with Doug B's advice - I've used that "list of forms with
different prefixes" technique and it works very well and the code
should be easy for others to follow down the road. You actually don't
need a prefix hidden field for each fieldset - one hidden field
containing the number of fieldsets is plenty if the prefixes follow a
sequence like 'room0','room1','room2' then loop as in the following
"notional code" (no validation, initialization, etc):

for i in range(int(request.POST['numberOfFieldSets'])):
    roomshared.append(RoomShared(request.POST,prefix='room%d' % i))

If you are interested in generating the forms dynamically, one way
I've done it is to define the RoomShared class by building a
dictionary of fields and then creating the class using the built-in
type() rather than using a Class declaration:

RoomShared = type('RoomShared',(django.newforms.Form,),
{ 'field_name1':field_declaration1,'field_name2':...})

It's important to add the field declarations to the dictionary in the
order they will be displayed in the form if you want to use shortcuts
like as_p(). newforms assigns a creation counter to each field when
it's declaration is processed and orders fields by their creation
counters when displaying the form.

Using type() to create a class is discussed in the context of dynamic
model creation at http://code.djangoproject.com/wiki/DynamicModels

Cheers,
Jeff

On Sep 14, 9:16 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> On Sep 13, 11:06 pm, Doug B <[EMAIL PROTECTED]> wrote:
>
> > Could you just use a collection of forms with different prefixes...
>
> Hi Doug,
>
> that's a good idea... but i'm still following the meta-programming
> route... i tried with a form factory function which is conceptually
> closer to what i thought but still doesn't work.
>
> This is what i tried:
>
> def room_form_factory(new_data):
>     room_type = IntegerField(widget=Select(choices=BEDROOM_TYPES))
>     room_price = IntegerField()
>     is_shared = BooleanField(required=False)
>
>     class RoomShared(Form):
>         pass
>
>     for k in new_data:
>         postfix = k[-3:]
>
>         if k.startswith('room_type'):
>             setattr(RoomShared, 'room_type_%s' % postfix, room_type)
>         elif k.startswith('room_price'):
>             setattr(RoomShared, 'room_price_%s' % postfix, room_price)
>         elif k.startswith('is_shared'):
>             setattr(RoomShared, 'is_shared_%s' % postfix, is_shared)
>
>     return RoomShared(new_data)
>
> And this is my view code:
>
> def add_share_room_details(request, object_id):
>
>     if request.method == 'POST':
>         new_data = request.POST.copy()
>         form = room_form_factory(new_data)
>
>         d = vars(form)
>         for kw in d:
>             print "%s = %s" % (kw, d[kw])
>
>         if form.is_valid():
>             return HttpResponse("Form is valid")
>         else:
>             return HttpResponse("Form NOT valid")
>     else:
>         pass
>
>     return render_to_response("house_share_room.html")
>
> Any other idea?
>
> Thanks,
> Lorenzo


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