How to get multiple values from a template and update multiple rows in a table? Django

后端 未结 1 1903
忘了有多久
忘了有多久 2020-12-11 10:18

I\'m fairly new to Django and Python. I have a inventory management website. I am trying to update multiple values in a database. User should be able to input updated stock

相关标签:
1条回答
  • 2020-12-11 10:58

    Do

    <input type="number" value="{{item.instock}}" name="item_{{item.id}}">
    

    Then

    if request.method == 'POST':
        data = request.POST.dict()
        data.pop('csrfmiddlewaretoken', None)
        for i in data.items():
            obj = Item.objects.get(id=i[0].split("_")[1])
            if not str(obj.instock) == str(i[1]): #here check int or char datatype since 1 not equal "1"
                obj.instock = i[1]
                obj.save()
    
    0 讨论(0)
提交回复
热议问题