Home > OS >  How to create multiple orderitems according to number of dictionaries in list?
How to create multiple orderitems according to number of dictionaries in list?

Time:08-22

This is the data I am sending through postman raw(json) section. And data = json.loads(request.body) and following data are same

data = [{'quantity': 2, 'service_id': 1, 'price': 2}, {'quantity': 2, 'service_id': 1, 'price': 2}, {'quantity': 2, 'service_id': 1, 'price': 2}]

@api_view(['GET', 'POST']) 
@csrf_exempt 
def create_order(request, *args, **kwargs): 
    if request.method == 'POST': 
        now = datetime.date.today() 
        order = Order.objects.create(date=now) 
        order.save() 
        orderid = order.id 
        order_id = orderid 
        data = json.loads(request.body)
        orderitemslist = [int(v) for lst in data for k,v in lst.items()] 
        quantity = orderitemslist[1] 
        service_id = orderitemslist[2]
        price = orderitemslist[3] 
        for item in orderitemslist: 
            orderitemcreate= OrderItem.objects.create(order_id_id=order_id, 
                                                quantity=quantity, 
                                                service_id_id=service_id, 
                                                price=price) 
            orderitemcreate.save() 
        return Response({"data created"}) 
         
    else: 
        order_qs = models.Order.objects.all().values_list() 
        OrderItem_qs = models.OrderItem.objects.all().values_list() 
        return Response({"Order":str(order_qs),"OrderItem":str(OrderItem_qs)})

I want to store data in db like if there are three [{'quantity': 2, 'service_id': 1, 'price': 2}, {'quantity': 2, 'service_id': 1, 'price': 2}, {'quantity': 2, 'service_id': 1, 'price': 2}] dictionaries in list. The loop should create 3 Orderitem and store each value in table. But it creates 9 orderitems because there are 9 colums(1 dict has 3 col) 3 dicts = 9 cols .Can anybody tell where i am wrong?

CodePudding user response:

You are doing double iteration here: orderitemslist = [int(v) for lst in data for k,v in lst.items()]. So you get 9 order items.

You have to iterate your data.

The following snippet should work:

@api_view(['GET', 'POST']) 
@csrf_exempt 
def create_order(request, *args, **kwargs): 
    if request.method == 'POST': 
        now = datetime.date.today() 
        order = Order.objects.create(date=now) 
        order.save()
        data = json.loads(request.body)
        for item in data: 
            OrderItem.objects.create(
                order_id_id=order.id, 
                quantity=item.get("quantity"), 
                service_id_id=item.get("service_id"), 
                price=item.get("price")
            ) 
        return Response({"data created"}) 
         
    else: 
        order_qs = models.Order.objects.all().values_list() 
        OrderItem_qs = models.OrderItem.objects.all().values_list() 
        return Response({"Order":str(order_qs),"OrderItem":str(OrderItem_qs)})

Also create() method is creating an object and saving it all in one step. Look at Django Docs

  • Related