Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Preserve annotate() fields in queryset #633

Merged

Conversation

devxoul
Copy link
Contributor

@devxoul devxoul commented Apr 29, 2022

MultilingualQuerysetManager doesn't respect dynamically selected fields using annotate(). This PR adds an availability to preserve annotated fields.

@last-partizan
Copy link
Collaborator

Looks good to me, please fix lint errors and we can merge this.

@devxoul devxoul force-pushed the devxoul/queryset-custom-annotation branch from c34f7cb to 435af00 Compare April 29, 2022 09:37
@devxoul
Copy link
Contributor Author

devxoul commented Apr 29, 2022

@last-partizan Thanks for the quick review! I fixed the lint error :)

@last-partizan last-partizan merged commit 6f2688f into deschler:master Apr 29, 2022
@devxoul devxoul deleted the devxoul/queryset-custom-annotation branch April 29, 2022 11:52
@rastytheamateur
Copy link

Hi,

by this, you have broken values_list() method.

In previous version 0.17.5, it was working well:

MyModel.objects.filter(relatedmodel__isnull=False).annotate(Count('id')).values_list(
    'id', 'name').order_by('name').query
>>> SELECT "app_mymodel"."name_en", "app_mymodel"."id" FROM "app_mymodel"
    INNER JOIN "app_relatedmodel" ON ("app_mymodel"."id" = "app_relatedmodel"."mymodel_id")
    WHERE "app_relatedmodel"."id" IS NOT NULL
    GROUP BY "app_mymodel"."id"
    ORDER BY "app_mymodel"."name_en" ASC

while in the newest version 0.17.6 you receive by the very same query a different SQL and results as well:

MyModel.objects.filter(relatedmodel__isnull=False).annotate(Count('id')).values_list(
    'id', 'name').order_by('name').query
>>> SELECT "app_mymodel"."id", "app_mymodel"."name_en", COUNT("app_mymodel"."id") AS "id__count" FROM "app_mymodel"
    INNER JOIN "app_relatedmodel" ON ("app_mymodel"."id" = "app_relatedmodel"."mymodel_id")
    WHERE "app_relatedmodel"."id" IS NOT NULL
    GROUP BY "app_mymodel"."id"
    ORDER BY "app_mymodel"."name_en" ASC

Note the difference in the SELECT clause. Incorrect order, incorrect number of fields and one incorrect field at all.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants