如何在QuerySelectField中仅返回dintinct / unique值?
def getRequestor():
u = Request.query.all()
return u
class filterRequests(Form):
requestor = QuerySelectField(u'Requestor',
query_factory=getRequestor,
get_label=lambda x: x.requestedBy,get_pk=lambda x: x.requestedBy)
我试图弄乱get_pk,但它似乎没什么帮助。
谢谢
答案 0 :(得分:0)
我对我的回答并不满意,但是使用selectfield而不是这样做:
在视图中:
@app.route("/request_management",methods=["GET","POST"])
@login_required
def Request_management():
# import pdb;pdb.set_trace()
form=filterRequests()
RB= list(set([h.requestedBy for h in models.Request.query.all()]))
RB.append('chet')
form.requestor.choices=zip(RB,RB)
形式:
class filterRequests(Form):
status= SelectField(u'Status?',coerce=int, choices=[(99,'All'),(0,'Lower Priority Request'), (1, 'Incomplete Request'), (2, 'Pending review'),
(3, 'Assigned'), (4, 'Complete'), (5, 'Rejected')],default=99)
requestor= SelectField(u'request',default='chet')
答案 1 :(得分:0)
听起来你想使用allow_blank
requestor = QuerySelectField(u'Team', query_factory=getRequestor, get_label='requestedBy', allow_blank=False, blank_text=(u'All'))
然后在你的视图中有一些逻辑,检查queryselectfield是否为空,如果是,则将其视为“全部”,或者从那一点开始用它做什么