Skip to content
Advertisement

How to access “count” value as dict/property in sqlalchemy with group_by?

I am making a very simple query using ORM SQLAlchemy, in which I expect to get a column (type) as well as its occurences for each value (count with group by).

I can access the type column by accessing the type property on the server object (as shown in the code provided).

I can also access the count column by accessing the index 0 on the server object.

My question is: is there a simple way to access the count column as a dict/property? For example: server.count

I’ve extensively inspected the server object using dir(), but I couldn’t find any properties that would return the count value.

JavaScript

Advertisement

Answer

Found the answer to my question after re-reading the official ORM tutorial

All I need to do is to set a label for the column for which there’s no corresponding property in my class.

So, instead of doing this:

JavaScript

I should do that:

JavaScript

Then I can access the total_count property (provided by the label function):

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement