I am a newbie I want to change selected data (food_status) and save it to selected column (food_status column). But it shows an error
for field in self._meta.concrete_fields: AttributeError: 'NoneType' object has no attribute '_meta'
in this part
OrderItem.save(food_status, update_fields=['food_status'])
models.py
class OrderItem(models.Model):
Table_No = models.IntegerField(blank=False)
FoodId = models.TextField()
Item = models.TextField()
Qty = models.IntegerField(blank=False)
Price = models.TextField()
Note = models.TextField(max_length=100, null=True)
OrderId = models.TextField(max_length=100, 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,
default="has been ordered")
views.py
def kitchen_view(request):
chef_view = OrderItem.objects.all()
if request.method == "POST":
food_status = request.POST.get("food_status")
OrderItem.food_status = food_status
OrderItem.save(food_status, update_fields=['food_status'])
return render(request, 'restaurants/kitchen_page.html', {'chef_view':chef_view})
kitchen_page.html
<form action="#" method="post">
{% 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.Status }}
<button type="button" class="close" data-dismiss="modal" aria-
label="Close"><span class="glyphicon glyphicon-pencil" aria-
hidden="true">×</span></button>
<select>
<option name="food_status" id="1" value="None">Has been
ordered</option>
<option name="food_status" id="2"
value="Cooked">Cooked</option>
<option name="food_status" id="3" value="Ready to be
served">Ready to be served</option>
<option name="food_status" id="4"
value="Done">Done</option>
</select>
<a href='' button onclick="myFunction()"><input
type="submit" value="Change Status"></button>
</td>
</tr>
</table>
{% endfor %}
</form>
The code should save the food_status to food_status database column based on the select option in html.
Anyone can help me? Really appreciate
You've almost got it, but just not quite in the right order. You can't do OrderItem.food_status = ...
before creating an OrderItem
instance. For example, this would work:
order_item = OrderItem()
order_item.food_status = food_status
order_item.save()
Try using the following instead, more complete:
def kitchen_view(request):
chef_view = OrderItem.objects.all()
if request.method == "POST":
food_status = request.POST.get("food_status")
order_item = OrderItem(food_status=food_status)
order_item.save()
return render(request, 'restaurants/kitchen_page.html', {'chef_view':chef_view})