<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 導航 - [索引](# "總目錄") - [下一頁](# "添加 Favicon") | - [上一頁](# "延遲加載視圖") | - [Flask 0.10.1 文檔](#) ? - [Flask 代碼模式](#) ? # 在 Flask 中使用 MongoKit 近些日子,使用基于文檔的數據庫而不是基于表的關系數據庫變得越來越流行。這一方案展示了如何使用文檔映射庫 MongoKit ,來與 MongoDB 交互。 這一方案的使用需要一個可用的 MongoDB 服務器,并且安裝有 MongoKit 庫。 使用 MongoKit 有兩種常用的方法,我們將會逐一介紹: ### 顯式調用 MongoKit 的默認行為是這種顯式調用的方法。這種方法跟 Django 或者 SQLAlchemy擴展顯示調用擴展大體精神是相同的。 下面是一個 app.py 模塊的例子: ~~~ from flask import Flask from mongokit import Connection, Document # 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']) ~~~ 要定義您的模型,只需編寫一個從 MongoKit 導入的 Document 類的子類。如果您已經看過了 SQLAlchemy 的方案,您可能會奇怪為什么這里沒有一個會話,甚至沒有定義 init_db 函數。一方面, MongoKit 并沒有類似會話這種東西。這有時會增加代碼量,但是同時也使得數據庫操作非常高效。另一方面, MongoDB 是沒有模式的。這意味著您在相同的插入查詢,可以使用不同的數據結構。 MongoKit 本身也是沒有模式的。但是實現了一些用來確保數據完整的驗證。 以下是一個文檔的例子 (您可以將這個也放進 app.py 文件里): ~~~ def max_length(length): def validate(value): if len(value) <= length: return True raise Exception('%s must be at most %s characters long' % length) return validate class User(Document): structure = { 'name': unicode, 'email': unicode, } validators = { 'name': max_length(50), 'email': max_length(120) } use_dot_notation = True def __repr__(self): return '<User %r>' % (self.name) # register the User document with our current connection connection.register([User]) ~~~ 這個例子向您展示了怎么定義您自己的結構(名為 structure)、一個最大字符長度的驗證器以及使用 Monkit 的一項名為 use_dot_notation 的特性。某人情況下MongoKit 按照字典的方式行為,但是將 use_dot_notation 為真之后,您可以像您在幾乎所有的 ORM 當中那樣,使用點運算符來分割屬性的方式訪問您的文檔。 向數據庫里添加數據的方法如下所示: ~~~ >>> from yourapplication.database import connection >>> from yourapplication.models import User >>> collection = connection['test'].users >>> user = collection.User() >>> user['name'] = u'admin' >>> user['email'] = u'admin@localhost' >>> user.save() ~~~ 注意,MongoKit 在列的類型方面有些嚴格,您必須使用一個通常的 unicode 來作為 name 和 email 的類型,而不是普通的 str 類型。 查詢也很簡單: ~~~ >>> list(collection.User.find()) [<User u'admin'>] >>> collection.User.find_one({'name': u'admin'}) <User u'admin'> ~~~ ### PyMongo 兼容層 如果您想直接使用 PyMongo 。 您也可以利用 MongoKit 實現。如果您希望應用程序實現最佳的表現,您也許希望使用這種方法。注意,例子并沒有展示配合 Flask 使用的具體方法。請參考上面 MongoKit 的例子代碼: ~~~ from MongoKit import Connection connection = Connection() ~~~ 插入數據可以使用 insert 方法。我們必須先獲得一個連接。這跟在 SQL 的世界使用表有些類似。 ~~~ >>> collection = connection['test'].users >>> user = {'name': u'admin', 'email': u'admin@localhost'} >>> collection.insert(user) ~~~ print list(collection.find())print collection.find_one({‘name': u'admin'}) MongoKit 將會為我們自動提交修改。 查詢數據庫,您要直接使用數據庫連接: ~~~ >>> list(collection.find()) [{u'_id': ObjectId('4c271729e13823182f000000'), u'name': u'admin', u'email': u'admin@localhost'}] >>> collection.find_one({'name': u'admin'}) {u'_id': ObjectId('4c271729e13823182f000000'), u'name': u'admin', u'email': u'admin@localhost'} ~~~ 返回的結果也同樣是類字典的對象: ~~~ >>> r = collection.find_one({'name': u'admin'}) >>> r['email'] u'admin@localhost' ~~~ 關于 MongoKit 的更多信息,請訪問[website](https://github.com/namlook/mongokit) [https://github.com/namlook/mongokit]. ? 版權所有 2013, Armin Ronacher.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看