Thanks Justin!
I just figured this out myself. I'll include the updated RoomForm
definition below since I've cleaned it up a bit and made a small fix to
the save function:
----------------------------------------------------------------------
from django.db.models.related import RelatedObject
class RoomForm(forms.Manipulator):
def __init__(self, num_chairs=3):
self.fields = [
forms.TextField(field_name='name', length=20, maxlength=30,
is_required=True),
]
# Add extra fields for potential related chairs
self.num_chairs = num_chairs
for i in range(num_chairs):
self.fields.append(
forms.HiddenField(field_name='chair.%d.id' % i, is_required=False) )
self.fields.append(
forms.TextField(field_name='chair.%d.style' % i, length=20,
maxlength=30, is_required=False) )
def get_related_objects(self):
room_field = [x for x in Chair._meta.fields if x.name ==
'room'][0]
return [ RelatedObject( Room, Chair, room_field ) ]
def save(self, new_data):
# Add the main Room object
r = Room( name = new_data['name'] )
r.save()
# Save any related chair objects containing data
for i in range(self.num_chairs):
if new_data['chair.%d.style' % i]:
c = Chair( room = r, style = new_data['chair.%d.style'
% i] )
c.save()
return r
As you pointed out, the main trick is to define working
get_related_objects() function.
I'm looking forward to newforms. Until it is ready, oldforms will do,
now that we've figured this little mystery out!
-Jonathan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---