2016-11-24 2 views
0

У меня есть две модели django для заказов и элементов заказа. Я прохожу через каждую строку таблицы в jquery и передаю результаты в View через ajax. Проблема в том, что для каждого вызова ajax это увольняют создает запись база данных для orders.I хочет создать соответствующие позиции заказа из таблицы заказа и rows.What я делаю wrong.ThanksКак правильно сохранить значения строк таблицы в django

// Взгляд

class ProductListView(TemplateView): 
    def post(self,request,*args,**kwargs): 
     if request.is_ajax(): 
      line_items = {} 
      product_id = request.POST.get("product_id") 
      price = request.POST.get("price") 
      quantity = request.POST.get("quantity") 
      subtotal = request.POST.get("subtotal") 
      grandtotal = request.POST.get("grandtotal") 
      products = CustomerPrice.objects.get(id=product_id) 
      product_name = products.product 

      line_items = { 
       "product_id":product_name, 
       "price":price, 
       "quantity":quantity, 
       "subtotal":subtotal, 
       "grandtotal":grandtotal, 
      } 


     Order.objects.place('Credit Card','Pending', 
       grandtotal,subtotal,125,line_items,request.user) 
     return super(ProductListView,self).get(request) 

// Менеджер по модели

class OrderManager(models.Manager): 
    def place(self,payment_method,payment_status, 
       grandtotal,sub_total,po_number,lineitems,username): 

     charge_amount = float(lineitems['grandtotal']) 
     order = self.create(customer=username, 
          sub_total=lineitems['subtotal'], 
          total = lineitems['grandtotal'], 
          charge_amount=charge_amount, 
          #payment_method=payment_method, 
          payment_status=payment_status, 
          order_number=po_number) 
          #billing_address=billing_address, 
          #updated_by=username, 
          #created_by=username) 

     OrderItem.objects.create(order=order, 
           product=lineitems['product_id'], 
           price=lineitems['price'], 
           quantity=lineitems['quantity'], 
           sub_total=lineitems['subtotal'], 
           total =lineitems['subtotal'], 
           #tax_rate=tax_rate, 
           #tax_method=tax_method, 
           updated_by=username, 
           created_by=username) 

     return order 

ответ

0

Я расскажу вам, как это должно быть реализовано. Сделайте один запрос на отправку ajax с полезной нагрузкой.

{customer: "", payment_method: "" ... line_items: [ { product_id: '101', quantity: '2', ... }, { product_id: '102', quantity: '1', ... }] }

Теперь в окне Джанго Создать Order объект с customer, payment_method детали и т.д., и цикл по каждой позиции и создать соответствующий OrderLineItem объект.

+0

Спасибо Rag Sagar позвольте мне попробовать – Mashaa

Смежные вопросы