2014-01-21 1 views
0

У меня есть несколько одинаковых имен. html, созданный с помощью jquery и json.Django же имя несколько входных вставок в DB

HTML

<input type="text" name="product_id" value="xyz" /> 
<p class="left"> 
    <label class="name">General</label> 
    <input type="hidden" value="2" name="location_id"> 
    <input type="text" name="location_stock" placeholder="Quantity" class="text small right" value="5"> 
</p> 
<p class="left"> 
    <label class="name">warehouse 1</label> 
    <input type="hidden" value="5" name="location_id"> 
    <input class="text small right" type="text" name="location_stock" placeholder="Quantity" value="7"> 
</p> 
<p class="left"> 
    <label class="name">warehouse 2</label> 
    <input type="hidden" value="6" name="location_id"> 
    <input class="text small right" type="text" name="location_stock" placeholder="Quantity" value="8"> 
</p> 

Json

$.getJSON(url, function(models) { 
    var options = ''; 
    for (var i = 0; i < models.length; i++) { 
     options += '<p class="left"><label class="name">' + models[i].fields['name'] + '</label></span> <input type="hidden" name="location_id" value="' + models[i].pk + '" /><input type="text" class="text small right" placeholder="Quantity" name="location_stock" /></p>' 
    } 
    $(".widget.stock_details").append(options); 
    $("#stock_locations option:first").attr('selected', 'selected'); 
}); 

Я хочу, чтобы вставить это тузд таблицы, как этот

product_id = xyz , location_id=2, stock=5 
product_id = xyz , location_id=5, stock=7 
product_id = xyz , location_id=6, stock=8 

Что такое метод?

def add_new(request): 
    product_id = request.POST.get('product_id') 
    location_id = request.POST.get('location_id') 
    stock = request.POST.get('location_stock') 
    p = Products_stock(product_id=product_id,location_id=location_id,stock=stock) 
    p.save() 
    response_data = {} 
    response_data['status'] = 'true' 
    response_data['message'] = 'success' 
    return HttpResponse(json.dumps(response_data), mimetype='application/javascript') 

ответ

2

У меня есть решение отсюда https://stackoverflow.com/a/4731596/1686265. Я изменил свое мнение так. благодаря @ Krish R.

def add_new(request): 
     product_id = Products.objects.get(code=code).id 
     x = request.POST.getlist('location_id') 
     y = request.POST.getlist('location_stock') 
     zipped = zip(x, y) 
     for x, y in zipped: 
      z = Product_stocks(product_id=product_id,warehouse_id=x,stock=y) 
      z.save() 
     response_data = {} 
     response_data['status'] = 'true' 
     response_data['message'] = 'success' 
     return HttpResponse(json.dumps(response_data), mimetype='application/javascript') 
Смежные вопросы