我正在使用烧瓶架和mongoengine为餐馆建立预订网站。
我的主要目标是使用json获取客户ID等于所需客户ID的所有预留对象
data = rzv.objects(restaurant=rest, customer=cdb.objects.get(id=request.args.get("customer-reservation"))).all()
当我尝试触发此查询时,json给了我一个错误:
mongoengine.errors.InvalidQueryError
我的预订模式如下:
class Reservations(document.Document):
restaurant = fields.ReferenceField(Restaurant)
customer = fields.ReferenceField(Customers)
shift_type = fields.EmbeddedDocumentField(Shifts)
room = fields.ReferenceField(Rooms)
time = fields.StringField()
covers = fields.IntField()
status = fields.StringField(default="wait")
desk = fields.EmbeddedDocumentField(Desks)
date = fields.DateTimeField()
sit_date = fields.DateTimeField()
end_sit_date = fields.DateTimeField()
cancel_date = fields.DateTimeField()
我的客户模型如下:
class Customers(document.Document):
title = fields.StringField()
full_name = fields.StringField()
first_name = fields.StringField()
last_name = fields.StringField()
telephone = fields.StringField()
visits = fields.StringField()
JSON:
$.getJSON("?customer-reservation=" + $(this).attr("data-id"), function (data) {
console.log(data);
reservationFill(data);
});
最后是观点:
if request.args.get("customer-reservation"):
data = rzv.objects(restaurant=rest, customer=cdb.objects.get(id=request.args.get("customer-reservation"))).all()
return data
查询这种情况的正确方法是什么。我必须使用过滤器吗?
答案 0 :(得分:5)
您应该拆分查询:
customer = Customers.objects(id=request.args.get("customer-reservation")).get()
data = Reservations.objects(restaurant=rest, customer=customer).all()
此外,您还需要对任何不匹配的客户进行错误处理。
答案 1 :(得分:2)
您可以在一行中使用in-value in list(值列表):
data = Reservations.objects(restaurant=rest,
customer__in=Customers.objects.filter(id="your filter id")).all()