flask sqlalchemy wtf QuerySelectField returning duplicates and add a default value not in the db

631 views Asked by At
  1. How do I return only dintinct/unique values in my QuerySelectField?

    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)
    

I tried to mess with get_pk, but it doesn't seem to help.

  1. How do i make the wtf form field default to a value "all" that is not a value in the column.

Thank you

2

There are 2 answers

0
Chet Meinzer On

I'm not satisfied with my answer, but using selectfield instead and doin this works:

in the view:

@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)

in the form:

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')
0
rob123 On

It sounds like you want to use allow_blank

requestor = QuerySelectField(u'Team', query_factory=getRequestor, get_label='requestedBy', allow_blank=False, blank_text=(u'All'))

then have some logic in your view that checks if the queryselectfield is blank, and if so, treats it as 'All', or whatever you want to do with it from that point forward