我是新手 我想更改选定的数据(food_status)并将其保存到选定的列(food_status 列)。但它显示错误
对于 self._meta.concrete_fields 中的字段: AttributeError:“NoneType”对象没有属性“_meta”
在这部分
OrderItem.save(food_status, update_fields=['food_status'])
模型.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>
代码应根据 html 中的选择选项将 food_status 保存到 food_status 数据库列。
有人可以帮助我吗?真的很欣赏
您已经差不多明白了,但只是顺序不太正确。在创建
OrderItem.food_status = ...
实例之前,您不能执行 OrderItem
操作。例如,这可以工作:
order_item = OrderItem()
order_item.food_status = food_status
order_item.save()
尝试使用以下内容,更完整:
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})