该 问题 与您的代码是在
.__init__方法。如果要
debug-watch/print()使用参数,则会注意到该参数
tablet实际上是的一个实例
Correspondent:
class Tablet_Correspondent(db.Model): def __init__(self, tablet=None, correspondent=None): print "in __init__: ", tablet, correspondent self.tablet = tablet self.correspondent = correspondent
其原因是SA创建新值的方式。从文档创建新价值:
当列表
append()事件(或setadd(),dictionary__setitem__()或标量赋值事件)被关联代理拦截时,它将使用其构造函数实例化“中介”对象的新实例,并将给定值作为单个参数传递。
在您的情况下,调用时
tab.correspondents.append(cor),将
Tablet_Correspondent.__init__使用单个参数调用
cor。
解? 如果只添加
Correspondents到中
Tablet,则只需在中切换参数即可
__init__。实际上,完全删除第二个参数。
但是,如果您还将使用
cor.tablets.append(tab),则需要显式使用的
creator参数,
association_proxy如上文链接的文档中所述:
class Tablet(db.Model, GlyphMixin): # ... correspondents = association_proxy('tablet_correspondents', 'correspondent', creator=lambda cor: Tablet_Correspondent(correspondent=cor))class Correspondent(db.Model, GlyphMixin): # ... tablets = association_proxy('correspondent_tablets', 'tablet', creator=lambda tab: Tablet_Correspondent(tablet=tab))


