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 ?
The connect() function returns a MongoClient object. Using list_database_names() method available to this object, we can retrieve number of databases on the server. It is also possible to obtain list of collections in a database, using list_collection_names() method.
Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.
MongoEngine defines a Document class. This is a base class whose inherited class is used to define structure and properties of collection of documents stored in MongoDB database. Each object of this subclass forms Document in Collection in database.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With