如何多次保存表单数据?

时间:2013-06-19 16:34:33

标签: django django-forms

我正在制作定期应用。我需要能够多次保存表单数据。如果我尝试这样做它不起作用:

for a in range(1, 10):
    foo.save()

1 个答案:

答案 0 :(得分:4)

答案比评论更好:

# foo is your (valid) form
# bar is your object's instance
bar = foo.save(commit=False)

for a in range(1, 10):
    bar.save() # save the object
    bar.pk = None # reset the pk, next iteration of the loop will save a new one

来源:Docs Almighty (1.4)