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

values() ignores current lang

Open fatal10110 opened this issue 10 years ago • 5 comments

>>> activate('en')
>>> Branch.objects.filter(pk=85017).values('name')
[{'name': 'Agriculture'}]
>>> Branch.objects.filter(pk=85017)[0].name
'Agriculture'
>>> 
>>> activate('ru')
>>> Branch.objects.filter(pk=85017).values('name')
[{'name': 'Agriculture'}]
>>> Branch.objects.filter(pk=85017)[0].name
'\u0421\u0435\u043b\u044c\u0441\u043a\u043e\u0435 \u0445\u043e\u0437\u044f\u0439\u0441\u0442\u0432\u043e'

fatal10110 avatar Oct 06 '15 16:10 fatal10110

Seems like there is some problem when combining it with django-mptt

>>> Country.objects.filter(pk=1).values('name').__class__
<class 'django.db.models.query.FallbackValuesQuerySet'>
>>> Branch.objects.filter(pk=85017).values('name').__class__
<class 'django.db.models.query.ValuesQuerySet'>

fatal10110 avatar Oct 06 '15 16:10 fatal10110

Branch is a mptt model? What is Branch.objects.__class__ and Country.objects.__class__?

zlorf avatar Oct 06 '15 18:10 zlorf

Yes, branch is MPTT model

>>> Country.objects.__class__
<class 'modeltranslation.manager.MultilingualManager'>
>>> 
>>> 
>>> Branch.objects.__class__
<class 'mptt.managers.TreeManager'>

fatal10110 avatar Oct 06 '15 19:10 fatal10110

That's the problem. TreeManager is used and not patched by modeltranslation. Are you sure that Branch model is registered for translation? Try to set MODELTRANSLATION_DEBUG = True in settings and see console output if there is Branch listed.

zlorf avatar Oct 06 '15 19:10 zlorf

@register(Branch)
class BranchTranslationOptions(TranslationOptions):
    fields = ('name', 'slug',)
modeltranslation: Registered 31 models for translation (..., Branch, ...) [pid: 96623].

I have a legacy django app ( in the same django project), there I have a model with the same name (Branch) but it have it's own translation.py file.

fatal10110 avatar Oct 06 '15 20:10 fatal10110