I'm using a framework that does pagination like this:
def get_count_query(self):
return self.session.query(func.count('*')).select_from(self.model)
def paginate(self):
... <irrelevant>...
count = self.get_count_query.scalar()
...
I want to override the get_count_query method to use my own query because I'm filtering some results and get_count_query just returns all elements in the table. Queries are created dynamically, for example one query could be:
Asset.query.join(StatusLabel).filter(StatusLabel.status == 'Deployable', or_(
Asset.assigned_to.isnot(None)),
Asset.deleted_at.is_(None))
I can count the elements in this query easily with query.count()
:
def get_count_query(self):
q = Asset.query.join(StatusLabel).filter(StatusLabel.status == 'Deployable', or_(
Asset.assigned_to.isnot(None)),
Asset.deleted_at.is_(None))
return q.count()
But this will fail once it reach the .scalar() method (and I cannot remove this method). So the question is: how can I apply func.count('*')
to an existing query?
Can I retrieve the filters from my query and apply them to the func.count('*')
query?
all() method. The Query object, when asked to return full entities, will deduplicate entries based on primary key, meaning if the same primary key value would appear in the results more than once, only one object of that primary key would be present.
_sa_instance_state is a non-database-persisted value used by SQLAlchemy internally (it refers to the InstanceState for the instance. While not directly relevant to this section, if we want to get at it, we should use the inspect() function to access it).
all() will return all records which match our query as a list of objects.
The statement ends by calling subquery() , which tells SQLAlchemy that our intention for this query is to use it inside a bigger query instead of on its own.
you can use select_from
with join
and filter
def get_count_query(self):
return self.session.query(func.count('*')).select_from(Asset).join(StatusLabel)\
.filter(StatusLabel.status == 'Deployable', or_(
Asset.assigned_to.isnot(None),
Asset.deleted_at.is_(None)))
with subquery
def get_count_query(self):
q = Asset.query.join(StatusLabel).filter(StatusLabel.status == 'Deployable', or_(
Asset.assigned_to.isnot(None)),
Asset.deleted_at.is_(None))
return self.session.query(func.count('*')).select_from(q.subquery())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With