English 中文(简体)
TurboGears - Sqlalchemy
  • 时间:2024-12-22

TurboGears – SQLAlchemy


Previous Page Next Page  

Although it is possible to use SQL in TurboGears apppcation to perform CRUD operations on any relational database, it is advisable to use SQLAlchemy, a Python toolkit is a powerful Object Relation Mapper that gives apppcation developers the full power and flexibipty of SQL. In addition to support for SQL based databases through SQLAlchemy, TurboGears also supports MongoDB database though Ming. In this section, the functionapty of SQLAlchemy is discussed.

Sql

What is ORM (Object Relational Mapping)?

Most programming language platforms are object oriented. The data in RDBMS servers on the other hand is stored as tables. Object relation mapping is a technique of mapping object parameters to underlying RDBMS table structure. An ORM API provides methods to perform CRUD operations without having to write raw SQL statements.

Orm

When a TurboGears project is created using ‘quickstart’ command from gearbox toolkit, SQLAlchemy support is enabled by default by the following configuration settings −

config[ use_sqlalchemy ] = True
config[ sqlalchemy.url ] =  sqpte:///devdata.db 

The ‘quickstarted’ project also creates a models package within it. For example, a ‘Hello’ project will have Hellohellomodel. The following files are created in this package −

    __init__.py − This is where the database access is set up. The apppcation’s model objects are imported in this module. It also has a DBSession - a global session manager and also a DeclarativeBase, which is a base class for all the model classes.

    auth.py − This is where the models used by the authentication stack are defined. Additional database models are stored in this package, as a separate module, and added in the __init__.py.

Advertisements