Speed up development with full-stack environments for every branch.

Learn More

Setting up SQLAlchemy in TurboGears minimal mode [Python]

Forked from TurboGears Forms Validation.

42 Runs 104 Views 22 Copies

Shows how to configured TurboGears with SQLAlchemy in minimal mode instead of using quickstart

Saved

Saved

amol 16

amol
published 3 years ago

import sys
from tg import TGController, AppConfig, expose, validate, redirect
from datetime import datetime
from .model import DBSession, SomeModel
    
class RootController(TGController):
  @expose('sampleapp.index')
  def index(self, **kw):

    entries = DBSession.query(SomeModel).all()
    return dict(entries=entries)

  @expose()
  def make_entry(self):
    DBSession.add(SomeModel(name='Created at %s' % datetime.utcnow()))
    return redirect('/')
  
config = AppConfig(minimal=True, root_controller=RootController())

# Models and DB configuration requires a pckage with a *model* module inside
config.package = sys.modules[__package__]

# As we are going to use a package, dotted templates support is better suited
config.use_dotted_templatenames = True
config.renderers = ['genshi']

config.use_toscawidgets2 = True
config.use_sqlalchemy = True
config.use_transaction_manager = True
config.DBSession = DBSession
config['sqlalchemy.url'] = 'sqlite:////tmp/test.db'


application = config.make_wsgi_app()
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).