我创建了两个模型,Basket
和BasketItem
,如下所示。篮子可以有很多篮子物品,所以我使用外键来建立这种关系。
您可以看到Basket
模型有一个updated_at
字段,其中包含上次更新购物篮时的时间戳。我希望在添加,删除或更新BasketItem
时更新此字段。
我添加了一个事件监听器,但我的解决方案看起来并不优雅。有没有更好的方法呢?
class Basket(Base):
__tablename__ = "baskets"
id = Column(String(45), primary_key=True, default=uuid4)
shop = Column(String(45), nullable=False)
currency_code = Column(String(3), nullable=False)
total = Column(String(15), nullable=False, default=0)
status = Column(Enum("open", "closed"), nullable=False, default="open")
created_at = Column(DateTime, default=datetime.datetime.utcnow)
updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.utcnow)
items = relationship("BasketItem", backref="basket")
class BasketItem(Base):
__tablename__ = "basket_items"
basket_id = Column(String(45), ForeignKey('baskets.id'), primary_key=True)
product_url = Column(String(90), nullable=False, primary_key=True)
quantity = Column(Integer, nullable=False)
@event.listens_for(BasketItem, 'after_insert')
@event.listens_for(BasketItem, 'after_update')
@event.listens_for(BasketItem, 'after_delete')
def basket_item_change(mapper, connection, target):
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
basket = db_session.query(Basket).get(target.basket_id)
basket.updated_at = datetime.datetime.utcnow()
db_session.commit()
答案 0 :(得分:1)
我觉得使用事件监听器很好但你可以做类似下面的事情。当您添加新的basketitem时,我确定您首先查询父项,然后将该子项添加到该父项。如果是,那么:
basket = Basket.query.get(id)
if basket:
basket.update()
# you can perform delete or update too
item = BasketItem(basket=basket, ...)
db.session.add(item)
db.session.commit()
class Basket(db.Model):
...
def update(self):
self.updated_at = datetime.utcnow()
db.session.add(self)
db.session.commit()