Django orm how to use values ​​and still work with modeltranslation

I am using django v1.10.2

I am trying to create dynamic reports where I store fields and conditions as well as basic orm model information in a database.

My code for generating dynamic report

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 it main_model_name

could be anything.

This works great, except the linked models are actually registered with django-modeltranslation

and their names are not displayed with the correct translation field.

So, for one of the reports main_model

there is ProductVariant

. ProductVariant

hasMany Pattern

.

My display columns: serial_number

, created_at

,pattern__name

The first two columns are the fields owned by the model ProductVariant

. The last ofPattern

The model 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')

      

Calling the query values()

does not return me the expected language zh_hans

for the field pattern__name

.

I've read the documentation about multilingual managers

at http://django-modeltranslation.readthedocs.io/en/latest/usage.html#multilingual-manager but I still don't know how to do it.

Keep in mind that it main_model

can be anything depending on what I store in the database.

+3


source to share





All Articles