OK here we are, had to switch approaches due to a bug with the column reflect event, to use the aforementioned __mapper_cls__ (had the name wrong), so I think you'll see this is a pretty open-ended way to control how something maps as you're given total access to mapper() here:
from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base, declared_attr from sqlalchemy.ext.declarative import DeferredReflection from sqlalchemy import event Base = declarative_base() e = create_engine("sqlite://", echo=True) e.execute(""" create table sample ( Id integer primary key, Name varchar, Description varchar, IsActive varchar ) """) class MagicMappyThing(DeferredReflection): @declared_attr def __mapper_cls__(cls): def map_(cls, *arg, **kw): props = kw.setdefault("properties", {}) for k, v in cls.__map__.items(): props[v] = cls.__table__.c[k] return mapper(cls, *arg, **kw) return map_ class Sample(MagicMappyThing, Base): __tablename__ = 'sample' __map__ = {'Id': 'id', 'Name': 'name', 'Description': 'description', 'IsActive': 'is_active'} def __init__(self, id, name, description, is_active): self.id = id self.name = name self.description = description self.is_active = is_active MagicMappyThing.prepare(e) s = Session(e) s.add(Sample(id=1, name='some name', description='foo', is_active='foo')) s.commit() On Aug 26, 2013, at 5:17 PM, Michael Bayer <mike...@zzzcomputing.com> wrote: > > On Aug 26, 2013, at 5:16 PM, Michael Bayer <mike...@zzzcomputing.com> wrote: > >> >> On Aug 26, 2013, at 4:35 PM, Praveen <praveen.venk...@gmail.com> wrote: >> >>> The problem with using Mixins is that you need to know the definition of >>> columns already for creating the mixin class. What I am trying to do is >>> more like get the definition dynamically on the fly.Take a look at this: >> >> here's a simple way to add reflection events which intercept the Column and >> add the .key of your choice, and saves on code by making use of the existing >> DeferredReflection mixin instead of hand-coding it. > > except it doesnt work yet, give me 10 minutes. >
signature.asc
Description: Message signed with OpenPGP using GPGMail