deschler / django-modeltranslation

Translates Django models using a registration approach.
BSD 3-Clause "New" or "Revised" License
1.34k stars 257 forks source link

Support for Subquery and other expressions in annotate() #728

Open celyes opened 2 months ago

celyes commented 2 months ago

Thank you for this awesome package.

The plugin works perfectly for our needs except, however, when used with the annotate() where the annotate contains a Subquery expression or an F expression, it doesn't use the current language. For example, if the Subquery asks for a field called 'title', it's supposed to query title_en or title_fr or whatever is the current language but instead, it raises this exception:

FieldError: Cannot resolve expression type, unknown output_field.

Once specifying the output_field, we get this exception:

 # FieldType is the type of the field specified. e.g: CharField
QuerySet.annotate() received non-expression(s): <django.db.models.fields.FieldType>

Any idea how to work around this issue?

last-partizan commented 2 months ago

Hi, the only workaround is to use translated field name instead of generic name.

But, you can look at the source code and try to add this feature. I'll gladly review and merge it.

celyes commented 2 months ago

Sure I'll add it @last-partizan. can you just direct me to where i should focus on so I can add it? thank you.

last-partizan commented 2 months ago

@celyes start by adding a test-case, and then - look at the MultilingualQuerySet source code, probably you need to override annotate method, or maybe some other places there.

celyes commented 2 months ago

Thank you

wookkl commented 1 month ago

Could you please share the test code that is causing the error? I would like to help resolve this issue as well.