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

Fallback on foreignkey don’t return translation when we use values method.

Open ghost opened this issue 9 years ago • 3 comments

When I try to keep translated string from foreign object by a queryset.values(), the query don’t fallback on translated field.

Use case:

class Foo(models.Model):
    id = models.AutoField(primary_key=True)
    term_name = models.CharField(…) #translated field

class FooOptions(TranslationOptions):
    fields = ('term_name',)
    empty_values = ''

translator.register(Foo, FooOptions)

class Bar(models.Model):
    id = models.AutoField(primary_key=True)
    foo = models.ForeignKey(Foo, …)

Foo.objects.values('term_name') return the translated value. Query: SELECT "foo"."term_name_fr", "foo"."term_name_en" FROM "foo"

But Bar.objects.values('foo__term_name') return the original value. Query: SELECT "foo"."term_name" FROM "bar" INNER JOIN "foo" ON ("bar"."foo_id" = "foo"."id")

ghost avatar Oct 27 '16 14:10 ghost

Ha, that's because Bar is not registered for translation, is it? And as a consequence, its manager is not multiligual-aware... Try registering Bar for translation with empty fields.

zlorf avatar Oct 27 '16 15:10 zlorf

Hi thx for the reply, that’s right Bar wasn't registered for translation. So I tried your solution but nothing change. Using values on foreignkey don’t try to retrieve translated field.

ghost avatar Oct 28 '16 07:10 ghost

Duplicate of #345

mwheels avatar Aug 07 '18 10:08 mwheels