Hi,
I use something similar to this. What I did was override the __init__
to take extra arguments with the instances it's editing and store them
for future use.
so it would look something like this:
class MyForm(forms.Form):
def __init__(self, objects=[], *args, **kwargs):
super(Form, self).__init__( *args, **kwargs )
self.objects = objects
for o in self.objects:
add some fields
def save( self ):
for o in self.objects:
get data from fields and call save()
in some cases I also add some empty "lines" for new objects:
class MyBetterForm(forms.Form):
def __init__(self, objects=[], *args, **kwargs):
super(Form, self).__init__( *args, **kwargs )
self.objects = objects
for o in self.objects:
add some fields
for i in range(4):
add some more fields
def save( self ):
for o in self.objects:
get data from fields and call save()
for i in range(4):
create some new objects
On 5/1/07, vega <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I'd like to understand how to manage newforms save when coping with
> a form
> that should edit both heading info and details. As an example let's
> think
> at an invoice splitted between invoice_headings/invoice_detail in a
> way similar to the following:
>
> class InvoiceHeading(Model):
> company = CharField(max_length=10)
> date = DateField()
> ...
>
> class InvoiceDetail(Model):
> invoice = ForeignKey(InvoiceHeading)
> qty = integerField()
> description = CharField(max_length=50)
>
> Let suppouse that we are only interested in a form that allows us to
> edit 3
> invoice rows. A form could resamble this one:
>
> class InvoiceForm(form_for_model()):
> def __init__(self):
> opts = {'required' : False}
> for row in range(3):
> self.fields['row_%s_qty' % (row)] = IntegerField(**opts)
> self.fields['row_%s_descr' % (row)] = CharField(maxlength=50,
> **opts)
>
>
> Now how should I define save()? I tried something similar:
>
> def save(self):
> self.invoice = super(InvoiceForm, self).save()
> for row in range(3):
> InvoiceDetail(qty=self.clean_data['row_%s_qty' % row],
> description=self.clean_data['row_%s_descr'
> %row],).save()
>
>
> This works when I create an object but how should I define it to
> make it
> able to update the object. This would just insert new rows each
> time.
>
> Should I use get_or_create? Is there a sample code for this kind of
> compound form?
>
> Thanks in advance
> *:-)
>
>
> >
>
--
Honza Kr�l
E-Mail: [EMAIL PROTECTED]
ICQ#: 107471613
Phone: +420 606 678585
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---