Differences between revisions 1 and 3 (spanning 2 versions)
Revision 1 as of 2016-10-05 04:22:17
Size: 32
Comment:
Revision 3 as of 2016-10-05 04:30:55
Size: 1168
Comment: pyramind mongdb
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Describe Pyramid/mongodb here. = MongoDB for Pyramid =
 *See a quick intro if you are exploring nosql,mongodb, or cassandra.
 *http://lucasmanual.com/blog/quick-intro-to-cassandra-vs-mongodb-with-python/
 *Normally you would:
{{{
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client.vinlib
a=recall.find({'YEARTXT':'2010'})
a=recall.find({'YEARTXT':'2010'})
}}}
 *In pyramid you do:
 *development.ini
{{{
mongo_uri = mongodb://localhost:27017/mydata
}}}
 *in __init__.py before config.scan() add:
{{{
    db_url = urlparse(settings['mongo_uri'])
    config.registry.db = MongoClient(
       host=db_url.hostname,
       port=db_url.port,
    )
    def add_db(request):
       db = config.registry.db[db_url.path[1:]]
       if db_url.username and db_url.password:
           db.authenticate(db_url.username, db_url.password)
       return db
    config.add_request_method(add_db, 'db', reify=True)
}}}
 *Now in our default.py
{{{
    #db is the database from URI, so in my case i already got mydata database, and what I'm accessing now is collection/table.
    mytable=request.db.recall
    print(mytable.find_one())
}}}

MongoDB for Pyramid

from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client.vinlib
a=recall.find({'YEARTXT':'2010'})
a=recall.find({'YEARTXT':'2010'})
  • In pyramid you do:
  • development.ini

mongo_uri = mongodb://localhost:27017/mydata
  • in init.py before config.scan() add:

    db_url = urlparse(settings['mongo_uri'])
    config.registry.db = MongoClient(
       host=db_url.hostname,
       port=db_url.port,
    )
    def add_db(request):
       db = config.registry.db[db_url.path[1:]]
       if db_url.username and db_url.password:
           db.authenticate(db_url.username, db_url.password)
       return db
    config.add_request_method(add_db, 'db', reify=True)
  • Now in our default.py

    #db is the database from URI, so in my case i already got mydata database, and what I'm accessing now is collection/table.
    mytable=request.db.recall
    print(mytable.find_one())

MyWiki: Pyramid/mongodb (last edited 2016-10-05 04:30:55 by LukaszSzybalski)