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

fields from related model not translating

Open Paul424 opened this issue 9 years ago • 6 comments

I found that fields from related models are not translating correctly when referenced via __ notation (don't know how to call it otherwise).

Example:

a.py

class A(models.Model): text = models.CharField(max_length=256) <= field is translated

b.py

class B(models.Model): a = models.ForeignKey('A', related_name='+')

view.py

import b

B.objects.all().values('a__text') <= not translating to current language B.objects.all().values('a__text_nl') <= is actually working!?

import a A.objects.all().values('text') <= correctly translating

Paul424 avatar Nov 19 '15 20:11 Paul424

+1. Same problem here! I am forced to use the B.objects.all().values('a__text__%s' % lang_code) technique.

Did you solve the problem another way Paul424 ??

manikos avatar May 06 '16 18:05 manikos

I'm doing something similar; retrieve only the specific field(s) based on the language code. Works fine for me. Paul Op 6 mei 2016 20:32 schreef "manikos" [email protected]:

+1. Same problem here! I am forced to use the B.objects.all().values('a__text__%s' % lang_code) technique.

Did you solve the problem another way Paul424 ??

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/deschler/django-modeltranslation/issues/345#issuecomment-217523505

Paul424 avatar May 06 '16 21:05 Paul424

Is there any fix to this that can be done in modeltranslation? There are a few circumstances where using the language specific field is a challenge.

dwasyl avatar Feb 16 '18 07:02 dwasyl

Hello dwasyl, Can you elaborate in what circumstances this workaround is a challenge? Paul

Paul424 avatar Feb 19 '18 07:02 Paul424

Sure @Paul424, mostly I ran into the issue when using modeltranslation with django-filter. django-filter operates by taking a list of fields and generating a list of possible values to filter the field by, for example if you specify relatedobj__name as a filter field it will get a list of values to generate the filter.

The work around was to subclass the class from django_filter and add a hook to look for specific field names that have translations and change the field name based on get_language(). Took a while to work out the issue and then how to change the third party package to add in the override.

dwasyl avatar Feb 19 '18 08:02 dwasyl

@dwasyl I am currently encountering the same problem using FilterSet. I appreciate your posted solution with subclassing, and I guess some other people may take advantage of your solution.

widnig89 avatar Aug 08 '18 07:08 widnig89