Le jeudi 17 décembre 2009 à 11:05 -0500, Michael Bayer a écrit :
> Chris Withers wrote:
> >
> > How should I create a class like this? This isn't about table
> > inheritance or the like and I'm *sure* I was told an easy solution for
> > this specific use case before, but I can't find it for the life of me
> > now...
> 
> 
> Just gave a current status on this feature three days ago:
> 
> http://groups.google.com/group/sqlalchemy/browse_thread/thread/99812e0ca1f8cc7c#

Another, not pretty, solution would be to use `exec` with a predefined
string containing the declarations...

Yet another solution is something like:

class CommonColumns(object):
    # Add here your column definitions

    @classmethod
    def update_locals(cls, locals):
        locals.update((k, v) for (k, v) in cls.__dict__.items()
                      if not k.startswith('_'))


class X(Base):
    # Add here the custom columns

    # Inject the common columns
    CommonColumns.update_locals(locals())


Or even:

class CommonColumns(object):
    # Add here your column definitions

    @classmethod
    def update_locals(cls):
        locals = sys._getframe(1).f_locals
        locals.update((k, v) for (k, v) in cls.__dict__.items()
                      if not k.startswith('_'))

class X(Base):
    # Add here the custom columns

    # Inject the common columns
    CommonColumns.update_locals()


--

You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


Reply via email to