AttributeError: 'NoneType' 对象没有属性 '_meta' Django

夏娃11

我是新手,我想更改所选数据(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")

视图.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})

厨房页面.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">&times;</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 数据库列。

任何人都可以帮助我吗?非常感激

NS0

您几乎已经掌握了它,但顺序不太正确。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})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Django Rest Framework: AttributeError: 'NoneType' 对象没有属性 '_meta' [for OneToOneField]

Django 1.8迁移-“ NoneType”对象没有属性“ _meta”

Django-AttributeError:“ NoneType”对象没有属性“ method”

AttributeError:'NoneType'对象没有属性'save'python-django

AttributeError:'NoneType'对象没有属性'attname'(Django)

Django Tincymce,AttributeError:“ NoneType”对象没有属性“ endswith”

Django:AttributeError:“ NoneType”对象没有属性“ split”

Django AttributeError:“ NoneType”对象没有属性“ has_header”

Django - AttributeError: 'NoneType' 对象没有属性 'pk'

Django makemigrations AttributeError:“ str”对象没有属性“ _meta”

Django AttributeError:“模型”对象没有属性“ _meta”

Django migration AttributeError:“ str”对象没有属性“ _meta”

Django:AttributeError:类型对象“ Position”没有属性“ _meta”

/addimam 'int' 对象的 AttributeError 没有属性 '_meta' Django

Django Group.objects 猴子补丁问题 - 'NoneType' 对象没有属性 '_meta'

AttributeError:'NoneType'对象没有属性'tbody'

AttributeError: 'NoneType' 对象没有属性 'group'

AttributeError: 'NoneType' 对象没有属性 'asfreq'

“ AttributeError:'NoneType'对象没有属性'insert'”

AttributeError:'NoneType'对象没有属性'update'

AttributeError:'NoneType'对象没有属性'text'?

AttributeError:'NoneType'对象没有属性'current'

AttributeError:'NoneType'对象没有属性'loader'

AttributeError: 'NoneType' 对象没有属性 '...'

AttributeError:'NoneType'对象没有属性'split'

AttributeError:'NoneType'对象没有属性'endswith'

AttributeError:'NoneType'对象没有属性'roles'

AttributeError:“ NoneType”对象没有属性“ _root”

AttributeError:“ NoneType”对象没有属性