我在SQLAlchemy中有一个表的类:
class pinCodes(db.Model):
id = db.Column(db.Integer, primary_key=True)
officename = db.Column(db.String(60), nullable=False)
pincode = db.Column(db.Integer, nullable=False)
taluk = db.Column(db.String(50), nullable=False)
district = db.Column(db.String(50), nullable=False)
state = db.Column(db.String(50), nullable=False)
__tablename__ = "pincodes"
def __init__(self, officename, pincode, taluk, district, state):
self.officename = officename
self.pincode = pincode
self.taluk = taluk
self.district = district
self.state = state
def __repr__(self):
return '<officename {}>'.format(self.officename)
现在我想用&#34; pincode&#34;返回所有列。作为特定值: 这是代码:
def fmt_pin(code):
return code.replace(" ","").replace("-","").replace("_","").strip()
def int_pin(code):
code = fmt_pin(code)
return int(code)
@app.route('/find_pin_codes', methods=['POST','GET'])
def pincodes():
if request.method == 'POST':
if request.form.get("pincode", "") != "":
pincode = request.form['pincode']
try:
num = pinCodes.query.filter(pincode = int_pin(pincode)).all()
return render_template('pincodes.html',
bypincode = num
)
except Exception as e:
return render_template('pincodes.html',
error = e)
但是当我搜索一个数字时,我收到以下错误: filter()得到了一个意想不到的关键字参数&#39; pincode&#39;。
编辑:我使用filter_by后,我没有得到任何结果。 这是我的Jinja模板代码:
{% if pincode is defined %}
<thead>
<tr>
<td><h4>Pincode</h4></td>
<td><h4>Office Name</h4></td>
<td><h4>Taluk</h4></td>
<td><h4>District</h4></td>
<td><h4>State</h4></td>
</tr>
</thead>
<tbody>
{% for o in bypincode %}
<tr>
<td><h5>{{ o.pincode }}</h5></td>
<td><h5>{{ o.officename }}</h5></td>
<td><h5>{{ o.taluk }}</h5></td>
<td><h5>{{ o.district }}</h5></td>
<td><h5>{{ o.state }}</h5></td>
</tr>
{% endfor %}
{% endif %}
答案 0 :(得分:1)
尝试使用filter_by
代替filter
。
filter_by(**kwargs)
正如您预期的那样工作:
pinCodes.query.filter_by(pincode = int_pin(pincode))
但是filter(*criterion)
没有关键字参数,你应该像这样使用它:
pinCodes.query.filter(pinCodes.pincode == int_pin(pincode))