[Grok-dev] Grok with existing SQLAlchemy mapped classes

Alberto Berti alberto at metapensiero.it
Mon Jul 27 12:31:53 EDT 2009


>>>>> "Wyatt" == Wyatt Baldwin <wyatt.lee.baldwin at gmail.com> writes:
    Wyatt> My only question (at this point, anyway) is whether there is
    Wyatt> a way to reuse existing SQLAlchemy-based model classes
    Wyatt> without having to add any Grok directives and/or
    Wyatt> Zope-specific integration code to them. The model for my
    Wyatt> application is in a "core" package that *cannot* be modified
    Wyatt> to support Grok (or any other Web framework) directly. One
    Wyatt> idea I have is a function that takes a SA mapped class and
    Wyatt> returns a Grok model.

    Wyatt> I've started looking at some of the libraries for integrating
    Wyatt> SA with Zope/Grok, but I admit that I haven't looked very
    Wyatt> deeply as yet. I've also been reading the Grok docs and this
    Wyatt> list but haven't found a definite answer yet. Is what I want
    Wyatt> to do already possible? Would it be straightforward to
    Wyatt> implement something like an `sa_model_wrapper`? Can I create
    Wyatt> a subclass of a SA model class that uses `grok.Model` as a
    Wyatt> mixin?

I did something like that for Dexterity which is very embrional... You
can indeed use grok.Model as a mixin, but you haven't really to subclass
from it, your class needs only to be marked to provide
grokcore.component.interfaces.IContext interface, which can be done with
a rule inside a configure.zcml file.

To attach to a db and to sync the SA session with zope's transaction
i've used z3c.saconfig which is rather simple to setup and it just does
this work, without forcing you to use some kind of SA binding.. any
binding that i've found has the implicit assumption that the developer
writes ORM classes while developing the web application, which is rather
dumb, in my opinion.

Then if you want to automate the conversion from SA's mapper
configuration to zope.schema fields to render forms,you can develop some
kind of column translator. There was one from objectrealms's svn repo
which was good enough, but i'm unable to access it right now... anyone
knows if it's still alive? Anyway i advise you to develop it so that it
writes down a skeleton in a py file, that you then will complete with
zope.schema's specific data... descriptions, sources, validation and so
on.

my two cents..

Alberto



More information about the Grok-dev mailing list