3

I have a problem with a more efficient searching, I have made code in different ways and do not convince me. I'm trying to find an employee with three filters, name, last name and department where he works.

the code in the view is as follows:

if form.nombre.data == u'' and form.apellido.data == u'' and form.departamento.data != u'':
    empleados = db.session.query(Empleado).filter_by(departamento_id=form.departamento.data).all()

elif form.nombre.data == u'' and form.apellido.data != u'' and form.departamento.data == u'':
    empleados = db.session.query(Empleado).filter_by(apellido=form.apellido.data.lower()).all()

elif form.nombre.data == u'' and form.apellido.data != u'' and form.departamento.data != u'':
    empleados = db.session.query(Empleado).filter_by(apellido=form.apellido.data.lower(),
                                                         departamento_id=form.departamento.data).all()

elif form.nombre.data != u'' and form.apellido.data == u'' and form.departamento.data == u'':
    empleados = db.session.query(Empleado).filter_by(nombre=form.nombre.data.lower()).all()

elif form.nombre.data != u'' and form.apellido.data == u'' and form.departamento.data != u'':
    empleados = db.session.query(Empleado).filter_by(nombre=form.nombre.data.lower(),
                                                         departamento_id=form.departamento.data).all()

elif form.nombre.data != u'' and form.apellido.data != u'' and form.departamento.data == u'':
    empleados = db.session.query(Empleado).filter_by(nombre=form.nombre.data.lower(), apellido=form.apellido.data.lower()).all()

elif form.nombre.data != u'' and form.apellido.data != u'' and form.departamento.data != u'':
    empleados = db.session.query(Empleado).filter_by(nombre= form.nombre.data.lower(), apellido=form.apellido.data.lower(), departamento_id=form.departamento.data).all()

else:
    empleados = db.session.query(Empleado).all()

As you can see it is a horrible code. if you were to add a filter more would be a combination of 16 statements if, let alone two more.

Any type of response is welcome. Thank you

2 Answers 2

1

Just build the query as you go, something like:

query = db.session.query(Empleado)

if form.nombre.data != '':
    query = query.filter_by(nombre=form.nombre.data.lower())
if form.apellido.data != '':
    query = query.filter_by(apellido=form.apellido.data.lower())
if form.departamento.data != '':
    query = query.filter_by(departamento_id=form.departamento.data)

print query.all()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. :D
0

You may want to use or_ or and_ filters. Like:

from sqlalchemy import or_
filter(or_(User.name == 'ed', User.name == 'wendy'))

See also tutorial and this post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.