开发者

Saving multiple forms efficiently using Django's Form Wizad and the issue of missing list indexes

I'm using Django's Form Wizard in my app. There are five forms in total however only three or four can be submitted, as two of the forms can be skipped based on a particular condition or from the two one form can be skipped based on a particular condition.

Contents of the list returned by SessionWizardView's done method is as follows:

[<test.myapp.models.AForm object at 0x0177EC10>, <test.myapp.models.BForm object at 0x01796150>, <test.myapp.models.CForm object at 0x01796530>, <test.myapp.models.DForm object at 0x01796910>, <test.myapp.models.EForm object at 0x01333540>]

To save the forms I am doing the following:

a = form_list[0].save(commit=False)
a.save()

b = form_list[1].save(commit=False)
b.parent_id = a
b.save()

c = form_list[2].save(commit=False)
c.parent_id = a
c.save()

d = form_list[3].save(commit=False)
d.parent_id = a
d.save()

d = form_list[4].save(commit=False)
d.parent_id = a
d.save()

As you will be able to tell, if only four forms are submitted, the list index 4 is non-existance and I get the "list 开发者_如何学JAVAindex out of range" error, like wise if only three forms are submitted the list index 3 and 4 are non-existance. My question is two fold; one how I can I overcome the issue of non-existant list indexes and two how can I make the code more efficient?

Many Thanks


um, you can iterate over yourform_listusingforstatement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜