django-modeltranslation
django-modeltranslation copied to clipboard
fields from related model not translating
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
+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 ??
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
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.
Hello dwasyl, Can you elaborate in what circumstances this workaround is a challenge? Paul
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 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.