2

用例:想要存储0.0为默认值,而没有为 a 传递任何peweeDoubleField。我编写了以下代码,但对我不起作用。

class MyRelation(peewee.Model):
    id = peewee.PrimaryKeyField()
    percentage = peewee.DoubleField(default=0.0)

这是一个api

@api_blueprint.route('/add_data', methods=['POST'])
@http_header
def add_data():
    try:
        incoming = json.loads(request.data)
        data = MyRelation(percentage=incoming["percentage"])
        data.save()

        return success_response(201,"Data has been inserted :)")

    except Exception as e:
        log(str(e))
        return raise_error(500, str(e))

记录以下错误

10-05-17 05:24:51 Line:199  Message: Error in add_data(),views.api: (1048, "Column 'percentage' cannot be null")
4

1 回答 1

3

差异仅在 Python 端强制执行——因此,如果您希望服务器强制执行默认设置,则需要使用约束:

percentage = peewee.DoubleField(constraints=[SQL('DEFAULT 0.0')])

文档:http ://docs.peewee-orm.com/en/latest/peewee/models.html#default-field-values

于 2017-05-08T17:46:51.753 回答