【发布时间】:2021-10-11 16:35:13
【问题描述】:
我想知道从模型中检索数据的正确方法是什么。我们以下面的类为例:
class A(db.Model):
def get_attributes():
return self.product_category.attributes
class Attribute(db.Mdel):
attribute_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
label = db.Column(db.String(255), nullable=False)
假设调用 get_attributes() 返回 Attribute 类的三个对象。
在我的路线中,我只想接收属性标签列表。我目前正在做的是遍历对象并检索标签属性,如下所示:
labels = [i.label for i in obj.get_attributes()]
我认为这不是正确的做法。有没有更好的方法来实现这一点?
【问题讨论】:
标签: python flask model flask-sqlalchemy crud