3

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.

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
  • 1
    It is probably better to write a patch for [django-modeltranslation](https://github.com/deschler/django-modeltranslation) package than to write a workaround solution, because the package seems not enough documented and there are related issues [Fallback on foreignkey don’t return translation when we use values method](https://github.com/deschler/django-modeltranslation/issues/392) or [Values and relationship spanning not translating](https://github.com/deschler/django-modeltranslation/issues/409). – hynekcer Aug 10 '17 at 22:01
  • When you call values() Django waits to get string static field-name, in your case its a pattern__name_zh or something. on display_columns you should pass fieldname+ '_lang', when you try to get value for default field name ORM gives you default lang field - en, as I know. – Oleksiy Aug 11 '17 at 08:16

0 Answers0