用于将记录添加到数据库的正确数据格式。烧瓶

时间:2019-06-25 07:42:51

标签: python flask sqlalchemy

我在sqlite数据库中添加了一个条目,其值来自查询的前端:

data[{"date_operation":"25/06/2019","code":"4","status":1,"type":3,"mesto_hraneniya_id":2,"contragent":"","sum_operation":"2","rezerv":"5","cnt_doc":"3","stellag":"23","id":"App.model.OperationModel-1","id_type":0}]

要创建一个条目,请在我的烧瓶应用程序的views.py中执行以下操作:

...
if method == 'Create':
        data = request.form.get("data")
        objFormat = json.loads(data)
        object = Operation()
        for key in objFormat:
            if 'date_operation' in key:
                key['date_operation'] = datetime.datetime.strptime(key['date_operation'], '%d/%m/%Y').date()
            dict_add = key
        for key in dict_add.keys():
            setattr(object, key, dict_add[key])            
        try:
            db.session.add(object)
            db.session.commit()                
            return (str(object.id))
        except SQLAlchemyError as e:
            error = {"data": e, "meta": {"success": "false"}}
            return (str(error))
...

就我而言,插入记录IntegrityError('(sqlite3.IntegrityError) datatype mismatch时出现错误db.session.add(object) 我的错误在哪里,以及如何添加记录? 谢谢

0 个答案:

没有答案