Hey,<br><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Cool! Could you share a bit of code on the list?</blockquote>
<div><br>Sure. but remember that I'm a newbie<br><br>I just try to follow what you did on your post + the doctest on zope.sqlalchemy + the rdbexample with collective.lead from the branch<br><br>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 string).<br>
<br>*** it was a long day for me, please let me know if this doesn't work. It's possible that I missed something<br><br>[app.py]<br><br>import grok<br><br>from sqlalchemy import *<br>from sqlalchemy.ext.declarative import declarative_base<br>
from sqlalchemy.orm import scoped_session, sessionmaker, relation<br>from zope.sqlalchemy import ZopeTransactionExtension, invalidate<br>import transaction<br><br>from models import *<br><br>_URL = 'postgres://grok:grok@localhost/grok'<br>
<br>engine = create_engine(_URL, convert_unicode=True)<br>Base.metadata.create_all(engine)<br><br>Session = scoped_session(sessionmaker(bind=engine,<br> twophase=True,<br> transactional=True,<br>
autoflush=True,<br> extension=ZopeTransactionExtension()))<br><br>class RDBExample(grok.Application, grok.Model):<br> def traverse(self, name):<br>
return Session().query(User).filter(User.name == name).first()<br><br>class Index(grok.View):<br> grok.context(RDBExample)<br> def render(self):<br> result = "Users on grok data base \n"<br>
for user in Session().query(User).all():<br> result += 'User: %s \n' % <a href="http://user.name">user.name</a><br> return result<br><br>class UserView(grok.View):<br> <a href="http://grok.name">grok.name</a>('index')<br>
grok.context(User)<br><br> def render(self):<br> return "You are over user: %s \n" % <a href="http://self.context.name">self.context.name</a><br><br>class AddUser(grok.AddForm):<br> grok.context(RDBExample)<br>
<br> @property<br> def form_fields(self):<br> return grok.Fields(IUser)<br><br> @grok.action('add')<br> def handle_add(self, *args, **kw):<br> user = User(**kw)<br> Session().save(user)<br>
<br><br>[models.py]<br><br>import grok<br>from sqlalchemy.ext.declarative import declarative_base<br>from sqlalchemy import *<br>from sqlalchemy.orm import relation<br><br>from interfaces import IUser, IAddress<br><br>Base = declarative_base()<br>
<br>class User(Base,grok.Model):<br><br> grok.implements(IUser)<br><br> __tablename__ = 'test_users'<br> id = Column(Integer, primary_key=True)<br> name = Column(String(50))<br> addresses = relation(backref="user")<br>
<br>class Address(Base, grok.Model):<br><br> grok.implements(IAddress)<br><br> __tablename__ = 'test_addresses'<br> id = Column(Integer, primary_key=True)<br> email = Column(String(50))<br> user_id = Column(Integer, ForeignKey('<a href="http://test_users.id">test_users.id</a>'))<br>
<br><br>[interfaces.py]<br><br>from zope import schema<br>from zope.interface import Interface<br><br>class IUser(Interface):<br> """<br> """<br> name = schema.TextLine(title=u"Name")<br>
<br>class IAddress(Interface):<br> """<br> """<br><br>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)""" )<br>
the method just shows the columns on the SpecialUser's table, but doesn't show anything from the User's table.<br><br>Is this a good practice? or should I modify the method on megrok.rdb???<br><br><br>Regards<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
Regards,<br><font color="#888888">
<br>
Martijn</font><div><div></div><div class="Wj3C7c"><br>
<br>
_______________________________________________<br>
Grok-dev mailing list<br>
<a href="mailto:Grok-dev@zope.org" target="_blank">Grok-dev@zope.org</a><br>
<a href="http://mail.zope.org/mailman/listinfo/grok-dev" target="_blank">http://mail.zope.org/mailman/listinfo/grok-dev</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Santiago Videla<br><a href="http://www.revolucionesweb.com.ar">www.revolucionesweb.com.ar</a><br><a href="http://www.linkedin.com/in/svidela">http://www.linkedin.com/in/svidela</a><br>
<br>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@s tod@s somos.