我有一个过滤表的表单,我希望表单条目可以构建sqlalchemy语句。
因为我有选项"无过滤器",我不能让表单字段进入过滤器(此处为'表单字段')。但是必须有一个比做每个排列更好的方法,比如
AT= list(set([h.assigned for h in models.Request.query.all()]))
AT.append('No Filter')
form.assigned.choices=zip(AT,AT)
requestlist= models.Request.query.all()
if form.validate_on_submit():
if form.status.data=='No Filter' and form.assigned.data=='No Filter' and form.requestor.data=='No Filter':
requestlist= models.Request.query.all()
elif form.status.data != 'No Filter' and form.assigned.data=='No Filter' and form.requestor.data=='No Filter':
requestlist = db.session.query(models.Request).filter(models.Request.status.like(form.status.data)).all()
elif form.status.data == 'No Filter' and form.assigned.data!='No Filter' and form.requestor.data=='No Filter':
requestlist = db.session.query(models.Request).filter(models.Request.assigned.like(form.assigned.data)).all()
elif form.status.data == 'No Filter' and form.assigned.data=='No Filter' and form.requestor.data!='No Filter':
requestlist = db.session.query(models.Request).filter(models.Request.requestedBy.like(form.requestor.data)).all()
elif form.status.data != 'No Filter' and form.assigned.data=='No Filter' and form.requestor.data!='No Filter':
requestlist = db.session.query(models.Request).filter(models.Request.requestedBy.like(form.requestor.data))\
.filter(models.Request.status.like(form.status.data)).all()
elif form.status.data == 'No Filter' and form.assigned.data !='No Filter' and form.requestor.data!='No Filter':
requestlist = db.session.query(models.Request).filter(models.Request.requestedBy.like(form.requestor.data))\
.filter(models.Request.assigned.like(form.assigned.data)).all()
elif form.status.data != 'No Filter' and form.assigned.data !='No Filter' and form.requestor.data =='No Filter':
requestlist = db.session.query(models.Request).filter(models.Request.status.like(form.status.data))\
.filter(models.Request.assigned.like(form.assigned.data)).all()
elif form.status.data != 'No Filter' and form.assigned.data !='No Filter' and form.requestor.data !='No Filter':
requestlist = db.session.query(models.Request).filter(models.Request.status.like(form.status.data))\
.filter(models.Request.requestedBy.like(form.requestor.data))\
.filter(models.Request.assigned.like(form.assigned.data)).all()
答案 0 :(得分:4)
您可以一个接一个地添加多个filter(...)
条款:
if form.validate_on_submit():
requestlist = models.Request.query
if form.status.data != 'No Filter':
requestlist = requestlist.filter(models.Request.status.like(form.status.data))
if form.assigned.data != 'No Filter':
requestlist = requestlist.filter(models.Request.assigned.like(form.assigned.data))
if form.requestor.data != 'No Filter':
requestlist = requestlist.filter(models.Request.requestedBy.like(form.requestor.data))
return requestlist.all()
或更一般地,您可以执行以下操作:
field_names = ['status', 'assigned', 'requestor']
if form.validate_on_submit():
requestlist = models.Request.query
for field in field_names:
f_value = getattr(form, field).data
if f_value != 'No Filter':
requestlist = requestlist.filter(getattr(models.Request, field).like(f_value))
return requestlist.all()
我看到的唯一问题是表单上的名称和模型有时不匹配。一个例子是requestedBy
和requestor
。在这种情况下,您可以在field_names
中定义字典而不是列表。