Sqlalchemy filter on object

Viewed 3486

I have a script that will test some conditions of objects in a SQLAlachemy ORM driven database. This will all be dynamic and that's given me some problems.

I want to query the minimum amount of objects for all tests, therefor all tests have a default filtering. If I were to test 2 tests, both filters will be applied to the query so I get the minimal amount of objects.

class Employee(Base):
    name = Column(String ...)
    account = Column(Account ...)
    coworkers = relationship(Employee_coworker, use_list=True ...)
    ...

So when I get all objects, I want to use my 2 filters

filter_1 = Employee.account != None
filter_2 = Employee.coworkers.any()
for e in session.query(Employee).filter(or_(filter_1, filter_2)).all():
    # IF e is here because of filter_1: 
    # Do something
    # IF e is here because of filter_2:
    # Do this thing
...

Is it possible to test an object by the same way a query gets filtered?

1 Answers
Related