The declared_attr attributes are called by the metaclass for IceCream before the creation of the IceCream class is complete, and therefore available in the module namespace, so you need to rely on cls to get at the appropriate context....but if you want Edible, you're probably best saying that explicitly here. I'm pretty sure the declared attr is only called in terms of its owning class but I'm not sure if we cover that behavior in tests.
Sent from my iPhone On Aug 29, 2011, at 3:42 AM, Fayaz Yusuf Khan <[email protected]> wrote: > Hi, I'm trying to call the base class attributes from a declared_attr > definition. > > Something like: > > class Edible(Base): > __tablename__ = 'edible' > brand = Column(String(20), primary_key=True) > edible_type = Column(String(20), primary_key=True) > __mapper_args__ = {'polymorphic_on': edible_type} > def __repr__(self): > return "<{}(brand={}, edible_type={})>".format( > self.__class__.__name__, self.brand, self.edible_type) > > > class IceCream(Edible): > __tablename__ = 'icecream' > brand = Column(String(20), ForeignKey(Edible.brand), primary_key=True) > cooling_recipe = Column(Text) > @declared_attr > def __mapper_args__(cls): > return { > 'polymorphic_identity': 'icecream', > 'inherit_condition': (super(IceCream, cls).edible_type > == 'icecream'), > 'inherit_foreign_keys': [super(IceCream, cls).edible_type]} > > but I'm getting nameerrors. > > Traceback (most recent call last): > File "super.py", line 17, in <module> > class IceCream(Edible): > File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py", > line 1165, in __init__ > _as_declarative(cls, classname, cls.__dict__) > File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py", > line 924, in _as_declarative > mapper_args = cls.__mapper_args__ > File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py", > line 1370, in __get__ > return desc.fget(cls) > File "super.py", line 25, in __mapper_args__ > 'inherit_condition': (super(IceCream, cls).edible_type > NameError: global name 'IceCream' is not defined > > Using super(cls, cls) or Edible instead of super(IceCream, cls) works. But > that doesn't sound right, does it?? > > How do I do this properly? > > Full code attached. > > -- > Fayaz Yusuf Khan > Cloud developer and designer > Dexetra SS, Kochi, India > fayaz.yusuf.khan_AT_gmail_DOT_com > fayaz_AT_dexetra_DOT_com > +91-9746-830-823 > <super.py> -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
