7

I have a model Product and a corresponding form Product and I need to update the stock with lets say 5 product, so I input the data for the Product and ask how many items of this product I want to store, because all 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 only saves the last form.

How else can I achieve what I need?

1

1 Answer 1

8

Calling save with commit=False returns an instance that is not saved to the database.

instance = form.save(commit=False)

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

for i in range(0, 5):
    instance.pk = None
    instance.save()
Sign up to request clarification or add additional context in comments.

1 Comment

thanks all i needed to do was put primary key to None. Thanks Much Appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.