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

Support for Subquery

Open mick88 opened this issue 1 year ago • 5 comments

I have the followinmg subquery that should annotate a user with their team name:

Subquery(Team.objects.filter(users__pk=OuterRef('pk')).order_by('order').values_list('name', flat=True)[:1])

However, this code fails for non-default locale because modeltranslation replaces values_list('name', flat=True) internally with values_list('name_en', 'name_es', flat=True)

This results in the following error when trying to annotate a queryset:

django.core.exceptions.FieldError: Cannot resolve expression type, unknown output_field

Defining output_field makes the error clearer:

django.db.utils.ProgrammingError: subquery must return only one column

mick88 avatar Sep 12 '23 15:09 mick88

Hi, thanks for reporting.

If you can, please try to dig this issue further and make PR with a fix.

last-partizan avatar Sep 13 '23 10:09 last-partizan

I've faced the same issue.

roboxdev avatar Feb 06 '24 19:02 roboxdev

Any solutions to this problem? any workarounds? @mick88 I'm facing the same issue

celyes avatar Apr 18 '24 10:04 celyes

No @celyes, I don't understand the code of this library well enough to contribute. I worked around the issue by not using a Subquery for now.

mick88 avatar Apr 18 '24 11:04 mick88

@mick88 that's a choice that I cannot make on my current project. The project I'm working on relies heavily on advanced features of the ORM so I'll either have to find a workaround or just ditch the library in favor of another one. Anyway, thanks for your reply. I appreciate it!

celyes avatar Apr 18 '24 12:04 celyes