Skip to content
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

model-translation doesn’t work with related tables #675

Open
ruchika2012 opened this issue Mar 10, 2023 · 8 comments
Open

model-translation doesn’t work with related tables #675

ruchika2012 opened this issue Mar 10, 2023 · 8 comments

Comments

@ruchika2012
Copy link

ruchika2012 commented Mar 10, 2023

Model-translation works well when i want to access column of the same model. But when it comes to relationship, it doesn’t work always give you the original model field and not the translated model field
Example (code snippet from views.py (method= GET))

queryset = Audit.objects.all().annotate(
          areaName=F("formFK__parentFK__tableFK__areaFK__name"))

name is a translated field in the area table, so it has 'name', 'name-es' and 'name-fr' columns since my translated languages are FR & ES

In this scenario, we are trying to access areaName via chaining through formFK. In this case the areaName gives the value from 'name' column instead of 'name-es', 'name-fr' (even when my current language is set either of es and fr)

It works fine when there's no relationship. Like if I have to read a translated field from Audit, it will read from corresponding column (created for that language).

Any ideas on how we can make GET work even if we are querying into related tables?

@last-partizan
Copy link
Collaborator

You can look at
https://github.com/deschler/django-modeltranslation/blob/master/modeltranslation/manager.py

I can see some code that looks like it should support translating related tables, but it it doesn't, create test case reproducing an issue, and then try to fix it.

@ryselis
Copy link

ryselis commented Mar 4, 2024

I have looked into the problem. In this case, Audit class is not registered for translations and does not use the translation queryset, and no field rewrites are performed. I see no other solution than to add translation queryset (or a different variant of it) for all related model hierarchy. Is this the approach we should go for?

@last-partizan
Copy link
Collaborator

Looks reasonable enough, yes.

@ryselis
Copy link

ryselis commented Mar 4, 2024

I don't think this should be closed yet - we will need a fix :)

@last-partizan
Copy link
Collaborator

Ok, leaving it open then.

But, preferable way to go - is to implement custom QuerySet yourself, test it for a few month, and if you think it's reasonably good - create PR.

@last-partizan last-partizan reopened this Mar 4, 2024
@ryselis
Copy link

ryselis commented Mar 4, 2024

I have implemented a custom queryset that handles only filtering as this was the problem for my application. The approach is to add a lazy operation to the translator that gets all non-registered models and registers the patched querysets with all the code copy-pasted from this library to handle only filter (_filter_or_exclude with all the dependencies). I will ship it to production and if all goes well, will maybe create a PR.

I don't see a better solution as the queries are generated inside of a queryset, so we need to patch it somehow, even though the model itself is not translatable.

@last-partizan
Copy link
Collaborator

The approach is to add a lazy operation to the translator that gets all non-registered models and registers the patched querysets

Why do you need to register all models?

Maybe, it would be better to specify manager explicitly where it's needed?

Something like

class Audit(...):
    objects = RecursiveModelTranslationManager()

@ryselis
Copy link

ryselis commented Mar 4, 2024

This works very weird with Django admin. I have two models - Good (has translatable title) and Balance (has a FK to Good). If I add title to GoodAdmin.search_fields, it searches by translated title. If I add good__title to BalanceAdmin.search_fields, it does not work with translated fields. It would still work in one language, making it more difficult to catch for QA. I think this is very error prone because it works differently from seemingly identical scenarios. Also if I do not have a translated field on Good and decide to make a field translatable later, there is no good way to find all models where I need this, and if I miss one, it will work inconsistently. Now add all possible ways to utilize this - annotate, order by etc. - it's almost impossible to catch all cases in a large application, especially when you have a long chain of FK fields, inherited models etc.

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

No branches or pull requests

3 participants