django ValueError:无法分配“值”“Order.dish_name”必须是“Dish”示例

b0zn9rqh  于 2023-03-13  发布在  Go
关注(0)|答案(1)|浏览(94)

我正在Django中编写一个餐厅应用程序,我有一个特定的视图来捕捉 AJAX 请求。从这个请求中我得到了一个菜的名称,我试图保存在一个新的对象Order中。我一直收到这个错误“ValueError:无法指定“意大利面":“Order.dish_name”必须是“Dish”示例。“我不知道为什么。我已经尝试修复它2天了,所以我想知道我做错了什么

type here

models.py网站

from django.db import models

class Table( models.Model ) :
    number = models.IntegerField( primary_key= True)

    def __str__( self ) :
        return str(self.number)
    
class Dish(models.Model ) :
    name = models.CharField( primary_key= True, max_length= 50 )
    price = models.FloatField()
    type = models.CharField( max_length= 50 )
    gluten = models.BooleanField( null= False)
    lactose = models.BooleanField( null= False)

    def __str__( self ) :
        return self.name

class Meal(models.Model ) :
    start = models.TimeField()
    end = models.TimeField(blank= True, null= True, default= None)
    table = models.ForeignKey(Table, on_delete=models.CASCADE)

    def __str__( self ) :
        return str(self.table.number)
    
class Order(models.Model):
    request_time = models.TimeField()
    kitchen_start_time = models.TimeField(blank= True, null= True, default= None)
    delivery_time = models.TimeField(blank= True, null= True, default= None)
    dish_name = models.ForeignKey(Dish, null= True, blank= True, default= None, on_delete=models.CASCADE)
    table = models.ForeignKey(Table, null= True, default= None, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.table.number)````

主. js

$(".special").click(function (e){
    var x= e.target.id
    $.ajax({
        type: 'POST',
        data: {
            'dishes' : x,
        },
        success: function(response){
            console.log(response);
            },
        error: function(error){
            console.log(error);
            },
    });   
    e.preventDefault();    
}
)

html的一部分

{% for d in dishes %}
      
        <div class="cell">
          <a href=# class="special" id='{{d.name}}' > {{d.name}}</a></li>
        </div>

  {% endfor %}

该视图

def open(request, number):

    tav = Table.objects.get(number = number)
    p = Meal(table=tav, start=datetime.now())
    p.save()
    dishes = Dish.objects.all().order_by('type')
    if request.method == 'POST':
        clicked_dish =request.POST.get('dishes')
        new_order= Order(request_time=datetime.now(), dish_name= clicked_dish, table=tav)
        new_order.save()

    return render(request, 'nuovo.html', {'dishes': dishes, 'table': tav})

并且URL

urlpatterns = [
    
    re_path(r'^admin/', admin.site.urls),
    re_path(r'home/', views.home),
    re_path(r'^new/',views.index, name='index'),
    re_path(r'^open/(\d+)/$',views.open, name='open'),
]
yhxst69z

yhxst69z1#

我会过滤掉一道菜

from django.http import HttpResponseBadRequest

def open(request, number):
    if request.method == "GET":
        return HttpResponseBadRequest()

    tav = Table.objects.get(number = number)
    p = Meal(table=tav, start=datetime.now())
    p.save()
    d =request.POST.get('dishes', None)
    dish = Dish.objects.filter(id=d).first()
     
    new_order= Order(request_time=datetime.now(), dish_name= dish, table=tav)
    new_order.save()

return render(request, 'nuovo.html', {'dish': dish, 'table': tav})

希望它的工作的!

相关问题