将我的 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")