You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tips.rst
+183
Original file line number
Diff line number
Diff line change
@@ -85,3 +85,186 @@ some of the allowed queries are
85
85
}
86
86
}
87
87
88
+
89
+
Aggregating
90
+
-----------
91
+
92
+
By default the `sqlalchemy.orm.Query` object that is retrieved through the `SQLAlchemyObjectType.get_query` method auto-selects the underlying SQLAlchemy ORM class. In order to change fields under the `SELECT` statement, e.g., when performing an aggregation, one can retrieve the `sqlalchemy.orm.Session` object from the provided `info` argument and create a new query as such:
# Wrap the results of the aggregation in `TypeCountBooksCoverArtist`
232
+
# objects.
233
+
objs = [
234
+
TypeCountBooksCoverArtist(
235
+
cover_artist=result[0],
236
+
count_books=result[1]
237
+
) for result in results
238
+
]
239
+
240
+
return objs
241
+
242
+
As can be seen, the `sqlalchemy.orm.Session` object is retrieved from the `info.context` and a new query specifying the desired field and aggregation function is defined. The results of the aggregation do not directly correspond to an ORM class so they're wrapped in the `TypeCountBooksCoverArtist` class and returned.
243
+
244
+
The `TypeStats` class can then be exposed under the `Query` class as such:
0 commit comments