Browse Source

fixed mongokit pattern to show how the flask way to configure hostname and port

pull/112/head
Christopher Grebs 14 years ago committed by Armin Ronacher
parent
commit
87c2c79442
  1. 23
      docs/patterns/mongokit.rst

23
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

Loading…
Cancel
Save