Exact match with filter() Flask

Question:

I’am new to python and iam coding a small webapp to search in a database. Its fonctionnal but doesnt function well.

Example : If i search for a ‘lieu’, a ‘Annee’ and a ‘Signataire’ it doesnt give me the exact matches, instead it gives me every ‘Signataire’ even if its not the ‘Annee’ or the ‘lieu’ that i was looking for.

Update : I changed to elif, it helps finding the right "signataire" but still gives me others "signataire" that comes with ‘lieu’ and ‘annee’ iam a bit lost.

Here is the code :

”’

def search_results(search):

    results = []
    lieu = search.data['Lieu']
    annee = search.data['Annee']
    signataire = search.data['Signataire']
    cote = search.data['Cote']


    if search.data['Lieu'] or search.data['Annee'] or 
    search.data['Cote']:
        qry = db_session.query(Cote).filter(and_(Cote.lieu.contains(lieu),Cote.annee.contains(annee),Cote.cote.contains(cote))
    )
        



    results = qry.all()

    elif search.data['Signataire']:
        qry = db_session.query(Cote).filter(or_(Cote.signataire_nom.contains(signataire),Cote.signataire_titre.contains(signataire)))
   
        results = qry.all()

    if not results:
        flash('No results !')
        return redirect('/')

    else:
        table = Results(results)
        table.border = True
        return render_template('results.html', table=table)

”’

Asked By: Jaybe999

||

Answers:

This is because you are using "if" the second time and not "elif". So if there exists the Signataire, then your results are always all the signataire.
To resolve this you should use something like this:

def search_results(search):

results = []
lieu = search.data['Lieu']
annee = search.data['Annee']
signataire = search.data['Signataire']
cote = search.data['Cote']


if search.data['Lieu'] or search.data['Annee'] or search.data['Cote']:
    qry = db_session.query(Cote).filter(and_(
        Cote.lieu.contains(lieu),Cote.annee.contains(annee),Cote.cote.contains(cote))
    )

    results = qry.all()

elif search.data['Signataire']:
    qry = db_session.query(Cote).filter(or_(
        Cote.signataire_nom.contains(signataire),Cote.signataire_titre.contains(signataire)))

    results = qry.all()

if not results:
    flash('No results !')
    return redirect('/')

else:
    table = Results(results)
    table.border = True
    return render_template('results.html', table=table)

Please add the indent to all the other lines except the define function line**

Answered By: Divyessh

I found a way to solve this. Thank you for your collaboration i appreciate.

Now i get the exact match.

I changed the code to this ->

”’
def search_results(search):

lieu = search.data['Lieu']
annee = search.data['Annee']
signataire_nom = search.data['Signataire_nom']
signataire_titre = search.data['Signataire_titre']
cote = search.data['Cote']
sujet_1 = search.data['Sujet_1']
sujet_2 = search.data['Sujet_2']
sujet_3 = search.data['Sujet_3']
categorie = search.data['Categorie']
conditions = [
    Cote.lieu.contains(lieu),
    Cote.annee.contains(annee),
    Cote.cote.contains(cote),
    Cote.categorie.contains(categorie),
    Cote.signataire_nom.contains(signataire_nom),
    Cote.signataire_titre.contains(signataire_titre),
    Cote.sujet_1.contains(sujet_1),
    Cote.sujet_2.contains(sujet_2), Cote.sujet_3.contains(sujet_3)

]
results = []


if search.data['Lieu'] or search.data['Annee'] or search.data['Cote'] or search.data['Categorie'] or search.data['Signataire_nom'] or search.data['Signataire_titre'] or search.data['Sujet'] :

    qry = Cote.query.filter(and_(*conditions))


    results = qry.all()

    if not results:

        flash('No results !')
        return redirect('/')

    else:
        table = Results(results)
        table.border = True

        return render_template('results.html', table=table)

”’

Jaybe

Answered By: Jaybe999