开发者

SQLAlchemy Custom Properties

I've got a table called "Projects" which has a mapped column "project". What I'm wanting to be able to do is to define my own property on my mapped class called "project" that performs some manipulation of the project value before returning it. This will of course create an infinite loop when I try to reference the row value. So my question is whether there's a way of setting up my table mapper to u开发者_运维技巧se an alias for the project column, perhaps _project. Is there any easy way of doing this?

I worked it out myself in the end. You can specify an alternative name when calling orm.mapper:

orm.mapper(MappedClass, table, properties={'_project': table.c.project})


Have you check the synonyms feature of Sqlalchemy

http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#defining-synonyms http://www.sqlalchemy.org/docs/05/mappers.html#synonyms

?

I use this pretty often to provide a proper setter/getter public API for properties having a pretty complicated underlaying data structure or in case where additional functionality/validation or whatever is needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜