python-使用AttributeExtension自动更新非标准化的属性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-使用AttributeExtension自动更新非标准化的属性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4872字,纯文字阅读大概需要7分钟。
内容图文
![python-使用AttributeExtension自动更新非标准化的属性](/upload/InfoBanner/zyjiaocheng/651/6ad0cf23af184a0eac382a0d9612bd38.jpg)
我在使用SQLAlchemy的AttributeExtension时遇到了一些麻烦.
实际上,我将非规范化的sum属性存储在Partent表中,因为出于排序目的,我经常需要它.但是,我希望每当其中一个子项的值更改时,该属性即可更新.
不幸的是,从未调用AttributeExtension的set()方法,因此无法识别更改.使用可以同时更新父级的属性设置器可能会起作用,但是我想知道如何正确使用SQLAlchemy的AttributeExtension(版本:0.6beta2).
这是一个小的(可运行的)代码段,它演示了该问题:
from sqlalchemy import create_engine, Column, Integer, ForeignKey
from sqlalchemy.orm import relation, scoped_session, sessionmaker, \
AttributeExtension
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///:memory:', echo=True)
session = scoped_session(sessionmaker(bind=engine, autoflush=True))
Base = declarative_base()
Base.query = session.query_property()
class ChildrenAttributeExtension(AttributeExtension):
active_history = True
def append(self, state, child, initiator):
parent = state.obj()
parent.sum_of_children += child.value
return child
def remove(self, state, child, initiator):
parent = state.obj()
parent.sum_of_children -= child.value
def set(self, state, child, oldchild, initiator):
print 'set called' # gets never printed
parent = state.obj()
parent.sum_of_children += -oldchild.value + child.value
return child
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'), nullable=False)
value = Column(Integer, nullable=False, default=0)
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
sum_of_children = Column(Integer, nullable=False, default=0)
children = relation('Child', backref='parent',
extension=ChildrenAttributeExtension())
Base.metadata.create_all(engine)
# Add a parent
p = Parent()
session.add(p)
session.commit()
p = Parent.query.first()
assert p.sum_of_children == 0
# Add a child
c = Child(parent=p, value=5)
session.add(c)
session.commit()
p = Parent.query.first()
assert p.sum_of_children == 5
# Change a child
c = Child.query.first()
c.value = 3
session.commit() # extension.set() doesn't get called
p = Parent.query.first()
assert p.sum_of_children == 3 # Assertion fails
谢谢你的帮助!
克里斯多夫
解决方法:
据我所知,您正在寻找有关child的事件,但要更改child.value.这样的事情应该可以解决问题:
class ValueAttributeExtension(AttributeExtension):
...
class Child(Base):
...
value = ColumnProperty(Column(Integer, nullable=False, default=0),
extension=ValueAttributeExtension())
EDIT-1:完整的工作示例如下:
from sqlalchemy import create_engine, Column, Integer, ForeignKey
from sqlalchemy.orm import relation, scoped_session, sessionmaker, AttributeExtension, ColumnProperty
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///:memory:', echo=False)
session = scoped_session(sessionmaker(bind=engine, autoflush=True))
Base = declarative_base()
Base.query = session.query_property()
class ValueAttributeExtension(AttributeExtension):
active_history = True
def append(self, state, child, initiator):
assert False, "should not be called"
def remove(self, state, child, initiator):
assert False, "should not be called"
def set(self, state, value, oldvalue, initiator):
print 'set called', state.obj(), value, oldvalue
child = state.obj()
if not(child.parent is None):
child.parent.sum_of_children += -oldvalue + value
return value
class ChildrenAttributeExtension(AttributeExtension):
active_history = True
def append(self, state, child, initiator):
print 'append called', state.obj(), child
parent = state.obj()
parent.sum_of_children += child.value
return child
def remove(self, state, child, initiator):
print 'remove called', state.obj(), child
parent = state.obj()
parent.sum_of_children -= child.value
def set(self, state, child, oldchild, initiator):
print 'set called', state, child, oldchild
parent = state.obj()
parent.parent.sum_of_children += -oldchild.value + child.value
#parent.sum_of_children += -oldchild.value + child.value
return child
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'), nullable=False)
value = ColumnProperty(Column(Integer, nullable=False, default=0),
extension=ValueAttributeExtension())
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
sum_of_children = Column(Integer, nullable=False, default=0)
children = relation('Child', backref='parent',
extension=ChildrenAttributeExtension())
Base.metadata.create_all(engine)
# Add a parent
p = Parent()
session.add(p)
session.commit()
p = Parent.query.first()
assert p.sum_of_children == 0
# Add a child
c = Child(parent=p, value=5)
session.add(c)
session.commit()
p = Parent.query.first()
assert p.sum_of_children == 5
# Change a child
#c = Child.query.first()
c.value = 3 # fixed bug: = instead of ==
session.commit() # extension.set() doesn't get called
p = Parent.query.first()
assert p.sum_of_children == 3 # Assertion is OK
内容总结
以上是互联网集市为您收集整理的python-使用AttributeExtension自动更新非标准化的属性全部内容,希望文章能够帮你解决python-使用AttributeExtension自动更新非标准化的属性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。