[Grok-dev] Re: SQLAlchemy integration

Santiago Videla santiago.videla at gmail.com
Wed May 21 01:37:20 EDT 2008


Cool! Could you share a bit of code on the list?

Sure. but remember that I'm a newbie

I just try to follow what you did on your post + the doctest on
zope.sqlalchemy + the rdbexample with collective.lead from the branch

this code depends on zope.sqlalchemy, SQLAlchemy>=0.4.6, psycopg2 and a
PstgreSQL database up and running of course (make sure to override the _URL

*** it was a long day for me, please let me know if this doesn't work. It's
possible that I missed something


import grok

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relation
from zope.sqlalchemy import ZopeTransactionExtension, invalidate
import transaction

from models import *

_URL = 'postgres://grok:grok@localhost/grok'

engine = create_engine(_URL, convert_unicode=True)

Session = scoped_session(sessionmaker(bind=engine,

class RDBExample(grok.Application, grok.Model):
    def traverse(self, name):
        return Session().query(User).filter(User.name == name).first()

class Index(grok.View):
    def render(self):
        result = "Users on grok data base \n"
        for user in Session().query(User).all():
            result += 'User: %s \n' % user.name
        return result

class UserView(grok.View):

    def render(self):
        return "You are over user: %s \n" % self.context.name

class AddUser(grok.AddForm):

    def form_fields(self):
        return grok.Fields(IUser)

    def handle_add(self, *args, **kw):
        user = User(**kw)


import grok
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import relation

from interfaces import IUser, IAddress

Base = declarative_base()

class User(Base,grok.Model):


    __tablename__ = 'test_users'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    addresses = relation(backref="user")

class Address(Base, grok.Model):


    __tablename__ = 'test_addresses'
    id = Column(Integer, primary_key=True)
    email = Column(String(50))
    user_id = Column(Integer, ForeignKey('test_users.id'))


from zope import schema
from zope.interface import Interface

class IUser(Interface):
    name = schema.TextLine(title=u"Name")

class IAddress(Interface):

I noticed that in the rdbexample branch that I was to following, there is a
method on megrok.rdb that translate the model table to a schema to make the
addForm. I used the interfaces instead, because when I try to make a form of
a sub-class (i.e: """class SpecialUser(User)""" )
the method just shows the columns on the SpecialUser's table, but doesn't
show anything from the User's table.

Is this a good practice? or should I modify the method on megrok.rdb???


> Regards,
> Martijn
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev

Santiago Videla

Sigue la mata dando de que hablar siempre abajo y a la izquierda donde el
pensamiento que se hace corazón resplandece con la palabra sencilla y
humilde que l at s tod at s somos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20080521/e613f25b/attachment-0001.html

More information about the Grok-dev mailing list