首先,使用forge_factory无法正确填充表单。我使用query_factory在表单中调用了一些类。当Flask加载此模板时,它不会加载我保存的参数。
views.py
@tasks.route('/edit/<int:task_id>', methods=['GET', 'POST'])
@login_required
def edit(task_id):
""" Edit task """
task = Task.query.filter_by(id=task_id).first_or_404()
form = TaskForm(obj=task, next=request.args.get('next'))
if form.validate_on_submit():
form.populate_obj(task)
db.session.add(task)
db.session.commit()
flash('Task updated', 'success')
return render_template('tasks/edit.html', task=task, form=form)
models.py
class Task(db.Model):
__tablename__ = 'Tasks'
id = Column(db.Integer, primary_key=True)
name = Column(db.String(120))
asset_id = Column(db.Integer, db.ForeignKey('Assets.id'))
asset = db.relationship("Asset", uselist=False, backref="task")
task_type_id = db.Column(db.Integer, db.ForeignKey('task_types.id'))
task_type = db.relationship("TaskType", uselist=False, backref="task")
task_status_id = db.Column(db.Integer, db.ForeignKey('task_statuses.id'))
task_status = db.relationship("TaskStatus", uselist=False, backref="task")
progress = Column(db.Float)
assigned_to_id = Column(db.Integer, db.ForeignKey('users.id'))
assigned_to = db.relationship("User", uselist=False, backref="task")
description = Column(db.Text())
start = Column(db.DateTime(), default=get_current_time)
due = Column(db.DateTime(), default=get_current_time)
task_closed = Column(db.Boolean, default=False)
forms.py
class TaskForm(Form):
next = HiddenField()
name = TextField(u'Task Name', [Required()])
asset_id = QuerySelectField(u'Asset',
query_factory=asset_query,
get_pk=lambda a: a.id,
get_label=lambda a: a.name,
allow_blank=False)
type_id = QuerySelectField(u'Type',
query_factory=type_query,
get_pk=lambda a: a.id,
get_label=lambda a: a.name,
allow_blank=False)
status_id = QuerySelectField(u'Status',
query_factory=status_query,
get_pk=lambda a: a.id,
get_label=lambda a: a.name,
allow_blank=False)
user_id = QuerySelectField(u'Assigned To',
query_factory=user_query,
get_pk=lambda a: a.id,
get_label=lambda a: a.name,
allow_blank=False)
start = DateField(u'Start Time', format='%m/%d/%Y')
due = DateField(u'Due Time', format='%m/%d/%Y')
description = TextAreaField(u'Description', [Required()])
submit = SubmitField('Add Task')
当我尝试在任务中保存更改时出错:
InterfaceError: (InterfaceError) Error binding parameter 0 - probably unsupported type. u'UPDATE "Tasks" SET asset_id=?, start=?, due=? WHERE "Tasks".id = ?' (<pursue.assets.models.Asset object at 0x10ebde050>, '2014-11-28 00:00:00.000000', '2014-11-28 00:00:00.000000', 1)