a = 23
b = 45
c = 67
query_result = User.query.filter(User.id == a, User.group == b, User.key == c).all()
query_result = User.query.filter(User.group == b, User.key == c).all()
from sqlalchemy import or_
query_result = User.query.filter(or_(a == None, User.id == a)
or_(b == None, User.group == b)
or_(c == None, User.key == c))
query_result = User.query
if a is not None:
query_result = query_result.filter(User.id == a)
if b is not None:
query_result = query_result.filter(User.group == b)
if c is not None:
query_result = query_result.filter(User.key == c)
query_result = query_result.all()
Find more questions by tags Python
That is, for example, the code
In ORM will look like:
Now our case. We substitute in the function of the condition of the first User.id == a and the condition of the second: a == None. The condition turns to True if the user id equals the value of a variable, or if variable a has the value None. You know? If a == None, this condition is always True, that is, it is as if ignored, exactly what you need:
and then the same for the rest of the fields. - tanya commented on July 9th 19 at 10:33