'_email': mytable.c.email
})
-It is also possible to use the `select_by` and `get_by` functions on `Query` using the original property name, by establishing a `synonym`:
+It is also possible to route the the `select_by` and `get_by` functions on `Query` using the new property name, by establishing a `synonym`:
{python}
mapper(MyClass, mytable, proeprties = {
# now you can select_by(email)
result = session.query(MyClass).select_by(email='john@smith.com')
+Synonym can be established with the flag "proxy=True", to create a class-level proxy to the actual property:
+
+ {python}
+ mapper(MyClass, mytable, proeprties = {
+ '_email': mytable.c.email
+ 'email' : synonym('_email', proxy=True)
+ })
+
+ x = MyClass()
+ x.email = 'john@doe.com'
+
+ >>> x._email
+ 'john@doe.com'
+
The `synonym` keyword is currently an [Alpha Feature][alpha_api].
#### Custom List Classes {@name=customlist}
"""return a new Mapper object."""
return Mapper(class_, table, *args, **params)
-def synonym(name):
+def synonym(name, proxy=False):
"""set up 'name' as a synonym to another MapperProperty."""
- return properties.SynonymProperty(name)
+ return properties.SynonymProperty(name, proxy=proxy)
def clear_mappers():
"""remove all mappers that have been created thus far. when new mappers are
class SynonymProperty(MapperProperty):
- def __init__(self, name):
+ def __init__(self, name, proxy=False):
self.name = name
+ self.proxy = proxy
def setup(self, querycontext, **kwargs):
pass
def execute(self, selectcontext, instance, row, identitykey, isnew):
pass
def do_init(self):
+ if not self.proxy:
+ return
class SynonymProp(object):
def __set__(s, obj, value):
setattr(obj, self.name, value)
sess = create_session()
mapper(User, users, properties = dict(
addresses = relation(mapper(Address, addresses), lazy = True),
- uname = synonym('user_name'),
- adlist = synonym('addresses')
+ uname = synonym('user_name', proxy=True),
+ adlist = synonym('addresses', proxy=True)
))
u = sess.query(User).get_by(uname='jack')