Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christopher Grebs · committed by Armin Ronacher · parent b5db6bf · Browse files · 1 file+17 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+17 added−6 removed

  • other:+17 added−6 removed

1 file+17 added−6 removed

modified:docs/patterns/mongokit.rst+17 added-6 removed
18 unmodified lines
1920The default behaviour of MongoKit is the declarative one that is based on21common ideas from Django or the SQLAlchemy declarative extension.2223Here an example `database.py` module for your application::23Here an example `app.py` module for your application::2425 from flask import Flask26 from mongokit import Connection, Document2727 connection = Connection()28 # configuration29 MONGODB_HOST = 'localhost'30 MONGODB_PORT = 270173132 # create the little application object33 app = Flask(__name__)34 app.config.from_object(__name__)3536 # connect to the database37 connection = Connection(app.config['MONGODB_HOST'],38 app.config['MONGODB_PORT'])394041To define your models, just subclass the `Document` class that is imported42from MongoKit. If you've seen the SQLAlchemy pattern you may wonder why we do43not have a session and even do not define a `init_db` function here. On the44one hand, MongoKit does not have something like a session. This sometimes
1 unmodified line
46MongoDB is schemaless. This means you can modify the data structure from one47insert query to the next without any problem. MongoKit is just schemaless48too, but implements some validation to ensure data integrity.4939Here is an example document (put this into `models.py`, e.g.)::50Here is an example document (put this also into `app.py`, e.g.)::5141 from yourapplication.database import Document, connection42 52 def max_length(length):53 def validate(value):54 if len(value) <= length:55 return True
50 unmodified lines
106PyMongo Compatibility Layer107---------------------------108109If you just want to use PyMongo, you can do that with MongoKit as well. You101may use this process if you need the best performance to get::110may use this process if you need the best performance to get. Note that this111example does not show how to couple it with Flask, see the above MongoKit code112for examples::113114 from MongoKit import Connection115116 connection = Connection()
28 unmodified lines