当我渲染表单时,整个ndb模型(PartModel即将到来。我希望它只渲染part_number属性,但实际存储PartModel。我已经通过WTForms文档挖掘了我和#39 ;我很难找到正确的方法来做到这一点。
有什么想法吗?相关文件如下所示。
new_dimension.html
< div class =" control-group">
< div class =" control-label"> {{form.dimension_part.label}}< / div>
< div class =" controls">
{{form.dimension_part | safe}}
{%if form.dimension_part.errors%}
< ul class ="错误">
{%表示form.dimension_part.errors%}中的错误
< li> {{error}}< / li>
{%endfor%}
< / UL>
{% 万一 %}
< / DIV>
< / DIV>
views.py
def list_dimensions(part_id = None):
"""列出尺寸"""
如果part_id不是None:
打印" ************零件ID是",part_id
Part = PartModel.get_by_id(part_id)
dimensions = DimensionModel.query(DimensionModel.dimension_part == Part.key).fetch()
title =" {}" .format(Part.part_name)的维度
其他:
dimensions = DimensionModel.query()#creates查询对象
title ="所有尺寸"
DimensionForm中的form = DimensionForm()#heaps
如果form.validate_on_submit():
dimension = DimensionModel(
dimension_part = form.dimension_part.data.key,
DIMENSION_NAME = form.dimension_name.data,
DIMENSION_VALUE = form.dimension_value.data,
dimension_usl = form.dimension_usl.data,
dimension_lsl = form.dimension_lsl.data,
added_by = users.get_current_user()
)
尝试:
dimension.put()
dimension_id = dimension.key.id()
flash(u' Dimension%s已成功保存。'%dimension_id,'成功')
返回重定向(url_for(' list_dimensions'))
除了CapabilityDisabledError:
flash(u' App Engine数据存储区目前处于只读模式。',' info')
返回重定向(url_for(' list_dimensions'))
return render_template(' list_dimensions.html',dimension = dimensions,form = form,title = title)#将查询对象推送到模板
forms.py
PartForm = model_form(PartModel,wtf.Form,field_args = {
' part_name':dict(validators = [validators.Required()]),
' part_number':dict(validators = [validators.Required()])
})
models.py
class PartModel(ndb.Model):
"""第二部分"""
part_name = ndb.StringProperty(required = True)
part_number = ndb.FloatProperty(required = True)
added_by = ndb.UserProperty()
timestamp = ndb.DateTimeProperty(auto_now_add = True)
class DimensionModel(ndb.Model):
"""尺寸"""
dimension_part = ndb.KeyProperty(required = True,kind = PartModel)
dimension_name = ndb.StringProperty(required = True)
dimension_value = ndb.FloatProperty(required = True)
dimension_usl = ndb.FloatProperty(required = True)
dimension_lsl = ndb.FloatProperty(required = True)
added_by = ndb.UserProperty()
timestamp = ndb.DateTimeProperty(auto_now_add = True)