Question

I'm building a reservation site for a restaurant using flask framework and mongoengine.

My main object is to fetch all the reservation objects which customer id's equal to wanted customer id with json

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

When i try to trigger this query json gives me an error:

mongoengine.errors.InvalidQueryError

My Reservation model 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 Customer model 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 right way to query this situation. Do i have to use a filter ?

Was it helpful?

Solution

You should split the query:

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

Also you will need error handling for any customers that don't match.

OTHER TIPS

You can use in – value is in list (a list of values should be provided) in a single line:

data = Reservations.objects(restaurant=rest, 
               customer__in=Customers.objects.filter(id="your filter id")).all()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top