How to save the same form more than once in Django 1.8?

I have a model product and the corresponding product form, and I need to update the stock if we say 5 products, so I enter the data for the Product and ask how many items of this product I want to store, because all the products to save are the same, except for Django default ID, I was thinking of doing something like this in the view:

for i in range(0, 5): form.save() 

Unfortunately, this retains only the latest form.

How else can I achieve what I need?

+5
source share
1 answer

A save call with commit=False returns an instance that is not stored in the database.

 instance = form.save(commit=False) 

You can save an instance several times in a loop. By setting the primary key to None, each new object will be saved.

 for i in range(0, 5): instance.pk = None instance.save() 
+4
source

Source: https://habr.com/ru/post/1233290/


All Articles