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 with a fake 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:
Copy Code code as follows:
From the Ming import session
From the Ming import schema
From Ming.orm import Mappedclass
From Ming.orm import Fieldproperty, Foreignidproperty, Relationproperty
From Ming.orm import threadlocalormsession
Session = None
def init_single_model (Model_class):
Model_class.__mongometa__.session = Session
Class Page (Mappedclass):
Class __mongometa__:
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))
Init_single_model (Page)
Mappedclass.compile_all ()
To initialize in project/config/environment.py:
Copy Code code as follows:
From.. Model Import Init_model
From Ming.datastore import datastore
def load_environment (global_conf, app_conf):
...
# Create The Mako templatelookup, with the default auto-escaping
config[' pylons.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 database engine
Init_model (Datastore (config[' Database.uri '))
# CONFIGURATION Options here (Note:all config options would override
# any pylons config options)
return config
Finally, add the MongoDB configuration entry in Development.ini:
Copy Code code as follows:
[App:main]
Database.uri = Mongodb://localhost:27017/test
If you need to initialize some data while the program is installed, you can add it in the project/websetup.py
Copy Code code as follows:
"" "Setup The Wukong Application" "
Import logging
Import Pylons.test
From. Config.environment Import load_environment
From. Import model
Log = Logging.getlogger (__name__)
def setup_app (Command, Conf, VARs):
"" "commands to setup Wukong" "
# Don ' t reload the app if it was loaded under the testing environment
If not Pylons.test.pylonsapp:
Load_environment (conf.global_conf, conf.local_conf)
Log.info ("Adding demo data.")
page = model. Page (title= ' demo ', content= ' this was for demo. ')
Model.session.flush ()
Log.info ("Successfully set up.")
This uses the Ming library to connect 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.