Search code examples
djangodatabaseinsert-update

Update data in database based on order id in Django


I am new in Django. I want to update the value in the database based on the order id. Therefore, every order id has different updates. But, i only can update the last item that i add to the order. And every previous orders that i have, will directly follow the update from the last item.

models.py

 class OrderItem(models.Model):
     Table_No = models.IntegerField(blank=False)
     FoodId = models.TextField()
     Item = models.TextField()
     Qty = models.DecimalField(max_digits=5, decimal_places=0)
     Price = models.DecimalField(max_digits=10, decimal_places=2)
     TotalPrice = models.TextField()
     Note = models.TextField(max_length=100, null=True)
     OrderId = models.TextField(max_length=5, null=True)

     FoodStatus = (
         ('1', 'Has been ordered'),
         ('2', 'cooked'),
         ('3', 'ready to be served'),
         ('4', 'done'),
     )
     food_status = models.CharField(max_length=50, choices=FoodStatus)

views.py

def kitchen_view(request):
    chef_view = OrderItem.objects.all()
    if request.method == "POST":
        order_id = request.POST.get("OrderId")
        status = OrderItem.objects.filter(OrderId=request.POST.get("OrderId")) 
        status.status1 = OrderItem.objects.update(food_status=request.POST.get("food_status"))
    return render(request, 'restaurants/kitchen_page.html', {'chef_view': chef_view})

kitchen_page.html

<form action="#" method="post">
    <style>
    table, th, td {
        border: 1px solid black;
    }
    </style>
        {% csrf_token %}                
        {% for order in chef_view %}    
            <table width="800">
                <tr>
                    <th width="800">Table Number</th>           
                    <th width="800">Item</th>                   
                    <th width="800">Quantity</th>               
                    <th width="800">Price</th>                  
                    <th width="800">Note</th>                   
                    <th width="800">Order Id</th>               
                    <th width="800">Status</th>                 
                </tr>
                <tr>
                    <td width="800">{{ order.Table_No }}</td>   
                    <td width="800">{{ order.Item }}</td>       
                    <td width="800">{{ order.Qty }}</td>    
                    <td width="800">{{ order.Price }}</td>      
                    <td width="800">{{ order.Note }}</td>       
                    <td width="800">{{ order.OrderId }}</td>    
                    <td width="800">{{ order.food_status }}     
                        <input type="text" name="food_status">
                </tr>
            </table>
        {% endfor %}
        <br><a href='' button onclick="myFunction()"><input type="submit" value="Change Status"></button>
    </form>

The result should be able to update the food_status based on the order_id. Therefore, every order_id may have different food_status and show it to the template.

Anyone can help me to solve this problem? I really need the help to solve this issue. Really appreciate.


Solution

  • Ok so real problem is that your form is incorrect - you are not sending OrderId to view. Here's quickfix to it:

    kitchen_page.html:

    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
    <form action="#" method="post">
        <table width="800">
            <tr>
                <th width="800">Table Number</th>
                <th width="800">Item</th>
                <th width="800">Quantity</th>
                <th width="800">Price</th>
                <th width="800">Note</th>
                <th width="800">Order Id</th>
                <th width="800">Status</th>
            </tr>
            {% csrf_token %}
            {% for order in chef_view %}
                <tr>
                    <td width="800">{{ order.Table_No }}</td>
                    <td width="800">{{ order.Item }}</td>
                    <td width="800">{{ order.Qty }}</td>
                    <td width="800">{{ order.Price }}</td>
                    <td width="800">{{ order.Note }}</td>
                    <td width="800">{{ order.OrderId }}</td>
                    <td width="800">{{ order.food_status }}
                        <input type="text" name="food_status" value="{{ order.food_status }}">
                </tr>
                <input type="hidden" name="OrderId" value="{{ order.OrderId }}">
            {% endfor %}
        </table>
        <br><button type="submit" value="Change Status">Change Status</button>
    </form>
    

    views.py:

    def kitchen_view(request):
        if request.method == "POST":
            order_ids = request.POST.getlist("OrderId")
            food_statuses = request.POST.getlist("food_status")
            for i in range(len(order_ids)):
                OrderItem.objects.filter(OrderId=order_ids[i]).update(food_status=food_statuses[i])
        chef_view = OrderItem.objects.all()
        return render(request, 'restaurants/kitchen_page.html', {'chef_view': chef_view})