Examples of using MongoDB in Python web framework pylons

Source: Internet
Author: User
Tags ming mongodb in python

  This article mainly introduces the use of MongoDB in the Python web framework pylons, you can use the reference

Pylons after a long development, finally released the 1.0 version. For formal product development, the 1.0 version of the meaning of a large, which indicates that the pylons API finally stabilized.   Pylons is born of a cottage Rails, but as a pure Python WEB framework, it has a distinct feature: strong customization. Each layer of the framework does not reinvent the wheel, but instead tries to integrate the existing Python libraries. In the MVC Model layer, pylons supports SQLAlchemy by default. Now NoSQL is very hot MongoDB. The application of MongoDB in pylons is also very simple. The following is a simple example.   Define MongoDB initialization functions and mapping objects in project/model/__init__.py:   code is as follows: From Ming import sessions   from Ming import s Chema from Ming.orm import Mappedclass to Ming.orm import Fieldproperty, Foreignidproperty, Relationproperty from MING.O RM Import Threadlocalormsession   session = None   def init_single_model (Model_class):     Model_class . __mongometa__.session = Session   class Page (Mappedclass):     class __mongometa__:       & nbsp Session = Session         name = ' pages '       _id = fieldproperty (schema. ObjectId)     title = Fieldproperty (str)     content = Fieldproperty (str)   def init_model (enGine):     Global session     session = Threadlocalormsession (Doc_session=session (engine))   &NB Sp Init_single_model (Page)     Mappedclass.compile_all ()       in project/config/environment.py To initialize:   Code as follows: from.. Model Import Init_model from Ming.datastore import datastore   def load_environment (global_conf, app_conf):   &N Bsp         # Create The Mako templatelookup, with the default auto-escaping     config[' pyl Ons.app_globals '].mako_lookup = templatelookup (        directories=paths[' templates '),         Error_handler=handle_mako_error,         Module_directory=os.path.join (app_ conf[' Cache_dir '], ' templates ',         input_encoding= ' utf-8 ', default_filters=[' escape '],         imports=[' from webhelpers.html import escape ']       # Setup the MongoDB datab ASE Engine &nbsP   Init_model (datastore (config[' Database.uri '))       # CONFIGURATION OPTIONS here (note:all config o Ptions'll override     # any pylons config options)       return config       last Add MongoDB configuration Items to Development.ini:   code as follows: [App:main] Database.uri = mongodb://localhost:27017/test     IF You need to initialize some data during the installation of the program, and you can add the     code in project/websetup.py as follows: "" "" "Wukong Application" "" Import logging   Import Pylons.test   from. Config.environment Import load_environment. Import model   log = Logging.getlogger (__name__)   def setup_app (command, Conf, VARs):     "" "Place" Y commands to setup Wukong here "" "    # Don ' t reload the app if it is loaded under the testing environment &NB Sp   If not Pylons.test.pylonsapp:         load_environment (conf.global_conf, conf.local_conf) & nbsp         Log.info ("adding demo data".")         page = model. Page (title= ' demo ', content= ' this are for demo. ')         Model.session.flush ()       &NB Sp Log.info ("Successfully set up.")     This uses the Ming library to connect to MongoDB and do a simple ORM. The Ming library is an ORM wrapper library for Pymongo. It is the SourceForge with TurboGears and MongoDB to reconstruct the website. It's a bit like SQLAlchemy ORM. In the example above, you can also replace Ming with an ORM library of Mongokit or other MongoDB, or even directly with Pymongo. There is a feeling that MongoDB will fire.  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.