使用WTForms在<select>下拉列表中渲染ndb模型属性

时间:2015-08-03 21:25:30

标签: python google-app-engine flask wtforms flask-wtforms

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

0 个答案:

没有答案