I am using django v1.10.2
I am trying to create dynamic reports whereby I store fields and conditions and the main orm model information into database.
My code for the generation of the dynamic report is
class_object = class_for_name("app.models", main_model_name)
results = class_object.objects\
.filter(**conditions_dict)\
.values(*display_columns)\
.order_by(*sort_columns)\
[:50]
So main_model_name
can be anything.
This works great except that the related models are actually registered with django-modeltranslation
and their names do not appear with the right translation field.
So for one of the reports main_model
is ProductVariant
. ProductVariant
hasMany Pattern
.
My display columns are :serial_number
, created_at
, pattern__name
The first two columns are fields that belong to ProductVariant
model.
The last one is from Pattern
Pattern model looks like this:
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Pattern(models.Model):
name = models.CharField(_('Pattern Name'), max_length=400)
serial_number = models.CharField(_('Pattern Number'), max_length=100, unique=True)
def __str__(self):
return str(self.name) + ' (' + str(self.serial_number) + ')'
class Meta:
verbose_name = _('Pattern')
verbose_name_plural = _('Patterns')
The queryset calling values()
does not return me the expected language zh_hans
for the field pattern__name
.
I read the documentation about multilingual managers
at http://django-modeltranslation.readthedocs.io/en/latest/usage.html#multilingual-manager but I still do not know how to make this work.
Bear in mind that the main_model
can be anything depending on what I store in the database.