我的模型是这种类型的字典: {"category":category, "dish_name":dish_name, "index": index} (索引仅用于强制产品以特定顺序显示)
在 Flask-Admin 中,我正在尝试自定义其表单,以便能够从我的类别(另一个 mongodb 集合)的下拉列表中更改产品的类别。我收到一个带有名称的文本字段和一个空的选择字段的提示。我认为这是因为模型实际上并不包含所有类别,所以表单“不知道”要显示什么。
然后我尝试覆盖 edit_form() 以强制表单中的类别列表,这样:
def edit_form(self, model):
try:
pk = self.get_pk_value(model)
if not pk:
raise ValueError('Document does not have _id')
choices = list(db.db.categories.find())
choices.sort(key=lambda x: x['order'])
sorted_choices = [(cat['name'], cat['name']) for cat in choices]
model['category'] = sorted_choices
return super(MyModelView, self).edit_form(model)
except Exception as ex:
flash(gettext('Failed to edit product. %(error)s', error=str(ex)),
'error')
return False
但我得到这个错误:
File "C:\Python27\lib\site-packages\flask_admin\model\base.py", line 1264, in edit_view
form = self.edit_form(obj=model)
TypeError: edit_form() got an unexpected keyword argument 'obj'
这让我很困惑,因为 'obj' 论点似乎一点也不意外......