djangodjango-querysetmodelchoicefield

Select data from drop-down list and save it to database in Django


I am a newbie in Django. I want to show the food_status in drop-down list options, therefore the chef can select one of them, change it, and update it into database. It can be updated into database, but i am not sure how to display the drop-down list on template based on the food_status that I have in models.py.

Anyone know how to do it?

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 see_order(request):
if request.method == "POST":
    OrderId = request.POST.get("OrderId")                           
    customerOrder = OrderItem(OrderId=OrderId)  
    so = OrderItem.objects.filter(OrderId=OrderId)  
    return render(request, 'restaurants/see_order.html', {'so': so}) 
else:
    return render(request, 'restaurants/customer_page.html')  

see_order.html

<form action="#" method="post">
<style>
table, th, td {
    border: 1px solid black;
    table-layout: fixed ;
    height: "2000" ;
    width: "2000" ;
}
</style>
    {% csrf_token %}                                
    {% for order in so %}                           
    <table>                                     
        <tr>
            <th>Table Number</th>               
            <th>Item</th>                       
            <th>Quantity</th>                   
            <th>Status</th>                     
            <th>Order Id</th>                   
        </tr>   
        <tr>
            <td>{{ order.Table_No }}</td>       
            <td>{{ order.Item }}</td>           
            <td>{{ order.Qty }}</td>            
            <td>{{ order.food_status }}</td>    
            <td>{{ order.OrderId }}</td>        
        </tr>
    {% endfor %}
    </table>
<br><input action="action" onclick="window.history.go(-1); return false;" type="button" value="Back"></br>
</form>

The kitchen_page template should show the drop-down list, then the chef can choose the food_status from that drop-down list, click save button, and update the database.


Solution

  • You can render choices using {% for %} loop and FoodStatus list of choices like this:

    <td>
        {{ order.get_food_status_display }}
        <select name="food_status">
            {% for id, choice in order.FoodStatus %}
                <option value="{{ id }}"{% if order.food_status == id %} selected="selected"{% endif %}>{{ choice }}</option>
            {% endfor %}
        </select>
    </td>
    

    You can display actual status text (instead of id), using get_FOO_display method.
    Added {% if %} tag to preselect correct option.
    Consider switching to Forms so it can handle rendering fields automatically.(!!!)
    Consider switching food_status to IntegerField instead. Provide default attribute, so it will always be one of the choices, even if not specified.