我已经坚持了一天多了。
型号:
class Reminder(db.Model):
__tablename__ = 'reminder'
reminder_id = db.Column('reminder_id', db.Integer, primary_key=True, autoincrement=True)
occasion_type_id = db.Column('occasion_type_id', db.Integer, ForeignKey('occasion_type.occasion_type_id'))
occasion_type = db.relationship("Occasion_Type", backref='reminder')
.... other fields
class Occasion_Type(db.Model):
__tablename__ = 'occasion_type'
occasion_type_id = db.Column('occasion_type_id', db.Integer, primary_key=True, autoincrement=True)
occasion_name = db.Column('occasion_name', db.String(100))
... other fields
表格
class ReminderAddForm(FlaskForm):
occasion_type = SelectField('Type of Occasion')
.... other form elements
编辑代码
def reminder_edit_modal(reminder_id):
reminder = Reminder.query.filter(Reminder.reminder_id == reminder_id).first()
form = ReminderAddForm(obj=reminder)
form.occasion_type.choices = occasion_choices()
# The next line works....
#form.occastion_type.data = reminder.occasion_type.occastion_type_id
有没有一种方法可以填充 occastion_type 选择字段而无需手动设置它(就像我在注释掉的行中所做的那样?)这只是更大设置的一小部分。如果我可以避免在编辑时为对象的相关部分手动预填充表单,这对我来说将是巨大的。
预先感谢任何可以提供帮助的人。