我正在使用烧瓶mongoalchemy进行演示,这是一个让我感到困惑的问题。首先,我创建一个类User
class Student(db.Document):
'''student info'''
name = db.StringField()
grade = db.IntField(required=False, default=None)
然后,我使用此数据库并创建一些示例,例如student1,student2 ...等。现在,我将等级的默认值从无更改为0
grade = db.IntField(required=False, default=0)
当我再次运行查询时,它引发错误:
mongoalchemy.exceptions.BadValueException: Bad value for field of type "grade". Reason: "Value is not an instance of <class 'int'>
SO,如何自动更新此字段更改?我现在要做的是手动修改数据库中的age值。
首先,要使查询像以前一样工作,请将字段上的allow_none
option设置为allow_none
。
True
这应该使没有等级值的文档在解包为python对象时不会触发错误。
下一步,进行迁移,将等级为“无”的文档的默认值设置为0。
最后,将字段声明切换回不允许等级的grade = db.IntField(required=False, default=0, allow_none=True)
值。