-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annotate related_object_count #50
Comments
Do you mean something like video_count_config = CountConfig(
related_model=Video,
join_condition=(Video.id == VideoSearchAssociation.video_id)
& (VideoSearchAssociation.search_id == Search.id),
count_alias='videos_count'
) Then passing it to a {
"data": [
{
"id": 1,
"term": "cats",
"videos_count": 5 // This Search has 5 associated Videos
},
{
"id": 2,
"term": "dogs",
"videos_count": 3 // This Search has 3 associated Videos
},
{
"id": 3,
"term": "birds",
"videos_count": 0 // This Search has no associated Videos
}
],
"total_count": 3 // Total number of Search records retrieved
} ? |
Yep, I think something like that would be perfect. Looks pretty flexible and extendable. In particular supports m2m relationship. It should be safe to always groupby the primary_model.id (as it's required by sqlalchemy syntax). |
I'm fixing this one next |
Sorry, this is taking a while. I'm fixing all the other issues related to joined methods before adding a new feature |
No worries, alot of new issues recently. Let's not force anything! |
Is your feature request related to a problem? Please describe.
I can't find a way to annotate number of related objects using the fastcrud.get_multi(). This is a raw sqlalchemy query I have instead: [note m2m relationship in this specific case]
Describe the solution you'd like
I'd like to be able to retrieve
videos_count
from search_crud.get_multi() method and potentialy others. Extendingto_select
with func.count(Video.id) seems relatively easy, but whole query must be wrapped in a GROUP_BY clause, which seems to be a bit harder to implement in an elegant way. Maybe some CountConfig resembling JoinConfig would be a way to go?The text was updated successfully, but these errors were encountered: