django-modeltranslation icon indicating copy to clipboard operation
django-modeltranslation copied to clipboard

Queryset filter doesn't evaluate translated field on ParentalManyToManyField

Open urlsangel opened this issue 4 years ago • 3 comments

I'm using Wagtail with django-modeltranslation, and ManyToManyField to form taxonomy relationships between pages and snppets with translated title and slug fields.

According to the docs, filtering on querysets should use the translated version of the field that I'm filtering on - in this case the slug field: https://django-modeltranslation.readthedocs.io/en/latest/usage.html#multilingual-manager

The SQL output below from django-debug-toolbar shows that a filter performed while the current language is set to French, the slug field is not looked up as slug_fr as expected, and the query fails to return pages related to this term.

Expected: taxonomies_resourcetype"."slug_fr" IN ('articles-de-recherche'))

Actual query (see below for full query): taxonomies_resourcetype"."slug" IN ('articles-de-recherche'))


SELECT ••• FROM "resources_resourcepage" INNER JOIN "wagtailtrans_translatablepage" ON ("resources_resourcepage"."translatablepage_ptr_id" = "wagtailtrans_translatablepage"."translatable_page_ptr_id") INNER JOIN "wagtailcore_page" ON ("wagtailtrans_translatablepage"."translatable_page_ptr_id" = "wagtailcore_page"."id") INNER JOIN "resources_resourcepage_resource_type" ON ("resources_resourcepage"."translatablepage_ptr_id" = "resources_resourcepage_resource_type"."resourcepage_id") INNER JOIN "taxonomies_resourcetype" ON ("resources_resourcepage_resource_type"."resourcetype_id" = "taxonomies_resourcetype"."id") WHERE ("wagtailcore_page"."path"::text LIKE '0001000200020001%' AND "wagtailcore_page"."depth" >= 4 AND NOT ("resources_resourcepage"."translatablepage_ptr_id" = 9) AND "wagtailcore_page"."depth" = 5 AND "wagtailcore_page"."live" AND "taxonomies_resourcetype"."slug" IN ('articles-de-recherche')) ORDER BY "resources_resourcepage"."published_date" DESC
--

My installed versions are:

Django==3.1
django-modeltranslation==0.15.1
wagtail==2.10

urlsangel avatar Sep 09 '20 09:09 urlsangel

Updated issue title and content, as the regular Django ManyToMany field has the same issue, and the extended version wasn't required.

urlsangel avatar Sep 09 '20 09:09 urlsangel

I can't offer many help here. If you want to get it working - try to figure out what is wrong with django-modeltranslation and create merge request.

last-partizan avatar Sep 09 '20 10:09 last-partizan

Hi @last-partizan

Thanks for the update, will do :-)

urlsangel avatar Sep 09 '20 10:09 urlsangel