将日期对象从用户输入传递到db(sqlalchemy)

时间:2019-05-10 13:26:05

标签: python flask sqlalchemy

到目前为止,我一直没有成功尝试将某些数据保存到数据库模型中。据我能找出其日期对象的问题,无论我做什么,都无法保存。日期是用户从表单中输入的内容。

@app.route('/upload/', methods=['GET', 'POST'])
def upload_file():
    if request.method=='POST':
        something = Schedule(
        start_time = request.form.get('start_time'),
        end_time = request.form.get('end_time'),
        duration =  request.form.get('duration'),
        action =  request.form.get('action'),
        location_name = request.form.get('location_name'), 
        vehicle_name = request.form.get('vehicle_name'),
        post_time = request.form.get('post_time'))
        db.session.add(something)
        db.session.commit()

    return render_template('index.html')

那是路线。

以下是模型

class Schedule(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    start_time = db.Column(db.String(20))
    end_time = db.Column(db.String(20))
    duration = db.Column(db.Integer) 
    action = db.Column(db.String(20))
    location_name = db.Column(db.String(64))
    vehicle_name = db.Column(db.String(20))
    post_time = db.Column(db.DateTime())

我访问路线时得到200,但是除了(id)字段自动递增外,仅添加一行数据。对于这个问题,我可能也完全错了。

2 个答案:

答案 0 :(得分:0)

尝试不使用__init__

something = Schedule()
something.start_time = request.form.get('start_time')
something.end_time = request.form.get('end_time')
something.duration =  request.form.get('duration')
something.action =  request.form.get('action')
something.location_name = request.form.get('location_name')
something.vehicle_name = request.form.get('vehicle_name')
something.post_time = request.form.get('post_time')
db.session.add(something)
db.session.commit()

答案 1 :(得分:0)

SQLite DateTime类型仅接受Python datetime和日期对象作为输入。假设您使用datetime-local作为属性输入类型,首先将字母T替换为空格,然后使用datetime.strptime将整个字符串解析为Python日期时间。

例如:

  

2019-05-21T17:29
T ”按字面意义显示在字符串中,以指示时间元素的开始,如ISO 8601中所指定

from datetime import datetime

time = request.form.get('post_time')
time_old = time.replace('T', ' ')
time_new = datetime.strptime(time_old, '%Y-%m-%d %H:%M')
post_time = time_new

更具体地说:

from datetime import datetime

class Schedule(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    start_time = db.Column(db.String(20))
    end_time = db.Column(db.String(20))
    duration = db.Column(db.Integer)
    action = db.Column(db.String(20))
    location_name = db.Column(db.String(64))
    vehicle_name = db.Column(db.String(20))
    post_time = db.Column(db.DateTime)


@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method=='POST':
        time = request.form.get('post_time')
        time_old = time.replace('T', ' ')
        time_new = datetime.strptime(time_old, '%Y-%m-%d %H:%M')
        something = Schedule(
        start_time = request.form.get('start_time'),
        end_time = request.form.get('end_time'),
        duration =  request.form.get('duration'),
        action =  request.form.get('action'),
        location_name = request.form.get('location_name'),
        vehicle_name = request.form.get('vehicle_name'),
        post_time = time_new
        )
        db.session.add(something)
        db.session.commit()

    return render_template('input.html')