1

将我的 Flask 应用程序更改为 Flask-Appbuilder,并遇到一些问题。我希望使用 url 中的参数更改 SelectField 的默认选择,有点像自动表单。From the following configuration, I get "/rooms/add?_flt_0_building=3" in the URL when the third building is selected. 如何在 WTForm 中获取 building_id 以在页面上选择他?

房间形式:

class RoomForm(DynamicForm):
  building_list = db.session.query(Building).all()
  name = StringField('Room name', validators=[DataRequired()])
  building_id = SelectField('Building', choices=[(r.id, r.name) for r in building_list], validators=[DataRequired()])
  floor_id = StringField('Floor', validators=[DataRequired()])

房间型号:

class Room(Model):
  id = Column(Integer, primary_key=True)
  name = Column(String(80), unique=True, nullable=False)
  building_id = Column(Integer, ForeignKey('building.id'), nullable=False)
  building = relationship("Building")
  floors_id = Column(Integer, ForeignKey('floors.id'), nullable=True)
  floor = relationship("Floors")
  node = relationship('Nodes', backref='roomkey', lazy='dynamic')
  device = relationship('Devices', backref='roomkey2', lazy='dynamic')

  def __repr__(self):
    return self.name

class Building(Model):
  id = Column(Integer, primary_key=True)
  name = Column(String(80), unique=True, nullable=False)
  floors = Column(Integer)
  gunitbuilding = relationship('Room', backref='buildingkey', lazy='dynamic')

  def __repr__(self):
    return self.name
    return self.id

房间景观:

class Rooms(ModelView):
  datamodel = SQLAInterface(Room)
  show_title = 'Rooms'
  add_title = 'Add room'
  edit_title = 'Edit room'
  list_title = 'Rooms'
  list_columns = ['name']
  show_fieldsets = [('Summary', {'fields':['name']} )]
  search_columns = ['name']
  add_columns = ['name','building_id','floor_id']
  add_form = RoomForm

class managerooms(MasterDetailView):
  datamodel = SQLAInterface(Building)
  list_title = 'Buildings'
  list_columns = ['name']
  related_views = [Rooms]
  show_title = 'Buildings'

appbuilder.add_view_no_menu(managerooms)
appbuilder.add_view_no_menu(Rooms)
appbuilder.add_link("Rooms", href="/managerooms/list/1?next=%2Fmanagerooms%2Flist%2F1", icon="", category="Manage")
4

1 回答 1

0

我想出了一个解决方法。如果有更好的方法请告诉我。我在 RoomForm 中添加了 refresh(),因为除了完整的 url、参数字符串之外,我不确定如何获取任何内容,因此我减去了前 19 个字符以获得正确的建筑物编号。

  def refresh():
    form = RoomForm()
    if request.method == 'GET':
      a=len(request.query_string)-19
      form.building_id.default = int(request.query_string[-a:])
      form.process()
    return form
于 2018-06-28T20:37:37.680 回答