Flask-SQLAlchemy使用request.form更新行

时间:2016-03-13 20:07:07

标签: python-2.7 flask sqlalchemy flask-sqlalchemy

我正在尝试更新数据库中的一行,但它不起作用。我不知道我在哪里犯了错误。

    Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/demo/demo.py", line 466, in dodajk1
    record.si = request.form['si']
AttributeError: 'NoneType' object has no attribute 'si'

我的models.py

class CC1(dbu.Model):
    __tablename__ = 'cc1'
    id = dbu.Column('id',dbu.Integer, primary_key = True)
    si = dbu.Column('si', dbu.Float)
    snd = dbu.Column('snd', dbu.Float)
    snh = dbu.Column('snh', dbu.Float)
    sno = dbu.Column('sno', dbu.Float)
    so = dbu.Column('so', dbu.Float)
    ss = dbu.Column('ss', dbu.Float)
    xa = dbu.Column('xa', dbu.Float)
    xh = dbu.Column('xh', dbu.Float)
    xi = dbu.Column('xi', dbu.Float)
    xnd = dbu.Column('xnd', dbu.Float)
    xp = dbu.Column('xp', dbu.Float)
    xs = dbu.Column('xs', dbu.Float)

 def __init__(self,si,snd,snh,sno,so,ss,xa,xh,xi,xnd,xp,xs):
                self.si = si
                self.snd = snd
                self.snh = snh
                self.sno = sno
                self.so = so
                self.ss = ss
                self.xa = xa
                self.xh = xh
                self.xi = xi
                self.xnd = xnd
                self.xp = xp
                self.xs = xs

最后是view.py:

@app.route('/dodajk1', methods=['POST'])
def dodajk1():
        if request.form['przycisk'] == 'Submit':
                record = dbu.session.query(CC1).get(1)
                record.si = request.form['si']
                record.snd = request.form['snd']
                record.snh = request.form['snh']
                record.sno = request.form['sno']
                record.so = request.form['so']
                record.ss = request.form['ss']
                record.xa = request.form['xa']
                record.xh = request.form['xh']
                record.xi = request.form['xi']
                record.xnd = request.form['xnd']
                record.xp = request.form['xp']
                record.xs = request.form['xs']
                dbu.session.commit()
        return redirect(url_for('k1'))

1 个答案:

答案 0 :(得分:1)

由于您只想更新一条记录,因此无需使用get。不要选择1作为主键的记录,而是选择第一条记录。

dbu.session.query(CC1).first()

由于您使用的是Flask-SQLAlchemy,因此可以将其缩短为

CC1.query.first()