Odmantic#

Quart-Mongo uses Odmantic for ODM (Object Document Mapper) with MongoDB.

Connection#

This is the same as for Motor. Refer to the information here.

Odmantic Engine Instance#

If the database name was passed with the URI. Then :attribute:`~quart_mongo.Mongo.odm` will be an instance of quart_mongo.wrappers.AIOEngine. If it was not, you will need to set the database manually like so.

mongo = Mongo(app)
mongo.odm = self.cx.odm['test-database']

Now you can follow the Odmantic documentation for inserting and finding documents.

Find One or 404#

The extension provides a helper function to find a document or it will raise a 404 error.

async AIOEngine.find_one_or_404(model: Model, *args, **kwargs) Model#

Find a single document or raise a 404 with the browser.

This function is like find_one(), but rather than returning None. It will raise a 404 error (Not Found HTTP status) on the request.

Parameters:
  • model – The Model to use to find the model.

  • args – Arguments to pass to find_one.

  • kwargs – Extra arguments to pass to find_one.

Raises:
  • HTTPException – Uses quart.abort to raises this exception if

  • there is not entry found in the database. This will raise a 404

  • error code with the browser.

Quart-Schema#

Quart-Schema can be used to send, recieve, and validate Odmantic models in routes, since Models is based on Pydantic for model definition and validation. More information Quart-Schema can be found here.

Note

If you are using Quart-Schema in your application. Please register Quart-Schema with application first and then Quart-Mongo. Quart-Mongo JSON provider should be used in lieu of Quart-Schema JSON provider. This will make sure that your application will use the JSON provider from Quart-Mongo. The JSON provider in Quart-Mongo has the same capability as the one from Quart-Schema except it can handle MongoDB collections as well.