Definitely simpler :) On Thu, Jan 19, 2017 at 16:23 mike bayer <mike...@zzzcomputing.com> wrote:
On 01/19/2017 01:53 PM, Samer Atiani wrote: > I would like to allow some of the CompositeProperty columns I'm building > to get their names dynamically, in a manner that mirrors how columns get > their names in sqlalchemy's declarative mapper. The best way I found to > implement this behavior is to do the following: > > class DeferredCompositeProperty(CompositeProperty): > def get_column_names(self, key): > raise NotImplementedError > > def undefer_column_names(self, key): > for col, name in zip(self.columns, self.get_column_names(key)): > col.name = name > col.key = name > > class CustomDeclarativeMeta(DeclarativeMeta): > def __new__(cls, name, bases, d): > def undefer(k, v): > if isinstance(v, DeferredCompositeProperty): > v.undefer_column_names(k) > > for k, v in d.iteritems(): > undefer(k, v) > > for base in bases: > for k, v in base.__dict__.iteritems(): > undefer(k, v) > > return DeclarativeMeta.__new__(cls, name, bases, d) > > class MoneyComposite(DeferredCompositeProperty): > def __init__(self): > DeferredCompositeProperty.__init__(self, <SomeCompositeClass>, > Column(Integer), Column(String)) > def get_column_names(self, key): > return [ > '{}_amount'.format(key), > ‘{}_currency’.format(key) > ] > Base = declarative_base(metaclass=CustomDeclarativeMeta) > > class ExampleModel(Base): > funds = MoneyComposite() > > > This would result in ExampleModel having the columns: funds_amount and > funds_currency automatically without having to specify them directly. > > Am I doing this right or is there a better way? not sure offhand - you really don't just want to say: funds = MoneyComposite('funds') ? sure would be simpler. > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google > Groups "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to sqlalchemy+unsubscr...@googlegroups.com > <mailto:sqlalchemy+unsubscr...@googlegroups.com>. > To post to this group, send email to sqlalchemy@googlegroups.com > <mailto:sqlalchemy@googlegroups.com>. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group. To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/snArVxEm4mA/unsubscribe. To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.