2016-11-24 94 views
0

錶行值我有訂單和二階Django模型items.I遍歷jQuery的每個錶行,將結果傳遞到通過ajax.The問題的看法是,對於每一個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', ... }] }

現在在django的視圖,通過每個行項具有customerpayment_method細節等,和環創建Order對象,並創建一個相應的對象OrderLineItem

+0

謝謝拉格薩加爾讓我試試 – Mashaa