From 87c2c794421f8a39aa98a35f518613cfd38852d5 Mon Sep 17 00:00:00 2001 From: Christopher Grebs Date: Sun, 27 Jun 2010 22:03:43 +0800 Subject: [PATCH] fixed mongokit pattern to show how the flask way to configure hostname and port --- docs/patterns/mongokit.rst | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/patterns/mongokit.rst b/docs/patterns/mongokit.rst index 5f6b3b94..27f182a3 100644 --- a/docs/patterns/mongokit.rst +++ b/docs/patterns/mongokit.rst @@ -20,11 +20,22 @@ Declarative The default behaviour of MongoKit is the declarative one that is based on common ideas from Django or the SQLAlchemy declarative extension. -Here an example `database.py` module for your application:: +Here an example `app.py` module for your application:: + from flask import Flask from mongokit import Connection, Document - connection = Connection() + # configuration + MONGODB_HOST = 'localhost' + MONGODB_PORT = 27017 + + # create the little application object + app = Flask(__name__) + app.config.from_object(__name__) + + # connect to the database + connection = Connection(app.config['MONGODB_HOST'], + app.config['MONGODB_PORT']) To define your models, just subclass the `Document` class that is imported @@ -36,10 +47,8 @@ MongoDB is schemaless. This means you can modify the data structure from one insert query to the next without any problem. MongoKit is just schemaless too, but implements some validation to ensure data integrity. -Here is an example document (put this into `models.py`, e.g.):: +Here is an example document (put this also into `app.py`, e.g.):: - from yourapplication.database import Document, connection - def max_length(length): def validate(value): if len(value) <= length: @@ -98,7 +107,9 @@ PyMongo Compatibility Layer --------------------------- If you just want to use PyMongo, you can do that with MongoKit as well. You -may use this process if you need the best performance to get:: +may use this process if you need the best performance to get. Note that this +example does not show how to couple it with Flask, see the above MongoKit code +for examples:: from MongoKit import Connection