How to register an array of data in Django

2

I have an array of data in my session storage related to products in a store, and I would like to register a sale as soon as the user registers all their products and clicks on the end of the sale.

This array of products is gaining a line with each new product registered in my sale.

How do I get this session storage and write to my database?

    
asked by anonymous 26.10.2015 / 13:27

1 answer

2

How is this product listing? Since it's a data list, you should use bulk_create for this:

#se sua listagem for um dict

produtos = request.session['produtos']
lista = []
for produto in produtos:
     lista.append(Produto(nome=produto['nome'], valor=produto['valor']))

Produto.objects.bulk_create(lista)

This will optimize the creation time. If there are 100 products to be created, it mounts the list and creates everything at once rather than creating one by one.

    
26.10.2015 / 13:52