Request mongoengine link to the field

I am creating a reservation site for a restaurant using a flask frame and a mongon.

My main object is to get all reservation objects whose client id is equal to the desired client id using json

data = rzv.objects(restaurant=rest, customer=cdb.objects.get(id=request.args.get("customer-reservation"))).all() 

When I try to call this request, json gives me an error:

 mongoengine.errors.InvalidQueryError 

My booking model is below:

 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() 

My client model is below:

 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); }); 

And finally, the view:

  if request.args.get("customer-reservation"): data = rzv.objects(restaurant=rest, customer=cdb.objects.get(id=request.args.get("customer-reservation"))).all() return data 

What is the correct way to query this situation. Should I use a filter?

+6
source share
2 answers

You must split the request:

 customer = Customers.objects(id=request.args.get("customer-reservation")).get() data = Reservations.objects(restaurant=rest, customer=customer).all() 

You will also need error handling for any clients that do not match.

+4
source

You can use in-value in the list (you must specify a list of values) in one line:

 data = Reservations.objects(restaurant=rest, customer__in=Customers.objects.filter(id="your filter id")).all() 
0
source

All Articles