【发布时间】:2015-03-31 20:46:26
【问题描述】:
我有三个表:components、attributes 和 attribute_values。每个组件可以有许多 attribute_values。每个 attribute_value 都属于一个 attribute。是的,这是可怕的 EAV 模式……
我创建了这两种形式:
class AttributeValueForm(Form):
attribute = HiddenField()
value = StringField('Value')
class ComponentForm(Form):
... non-related fields left out ...
attribute_values = FieldList(FormField(AttributeValueForm))
这些是 SQLAlchemy 模型:
class Component(db.Model):
__tablename__ = 'components'
id = db.Column(db.Integer, primary_key=True)
... non-related columns left out ...
class AttributeValue(db.Model):
__tablename__ = 'attribute_values'
id = db.Column(db.Integer, primary_key=True)
value = db.Column(db.String)
attribute_id = db.Column(db.Integer, db.ForeignKey('attributes.id'))
attribute = db.relationship('Attribute', backref='attribute_values'))
component_id = db.Column(db.Integer, db.ForeignKey('components.id'))
component = db.relationship('Component', backref='attribute_values'))
def Attribute(db.Model):
__tablename__ = 'attributes'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60))
我的问题是我希望将属性的 name 视为值字段的标签(替换“值”)。我一直在尝试了解 WTForms 如何在内部工作,但我看不到任何明显的方法。
任何提示表示赞赏。如果我只能在呈现值字段时获取 AttributeValue 对象,我什至可以使用仅呈现自定义标签的 hack。
【问题讨论】:
标签: flask wtforms entity-attribute-value