2

I have simple app with:

class NamedModel(TranslatableModel):
    code = models.CharField(max_length=120)

    translations = TranslatedFields(
        name=models.CharField(max_length=256),
        description=models.TextField()
    )

    class Meta:
        abstract = True


class Product(NamedModel):
    translations = TranslatedFields()


class Report(TranslatableModel):
    quantity = models.DecimalField(max_digits=20, decimal_places=6)
    inv = models.ForeignKey(Inv)
    translations = TranslatedFields(
        product=models.ForeignKey(Product)
    )

And then, from shell I creating some instances:

product = Product.objects.language('en').create(name='asd', description='asd', code='asd')

inv = Inv.objects.create(code='xxx')

report = Report.objects.language('en').create(quantity=1, inv=inv, product=product)

And if I type: len(Report.objects.language('en').all()), I get 1, but when I try this:

Report.objects.language('en').all()

I get:

NoTranslationError: Accessing a translated field requires that the instance has a translation loaded, or a valid translation in current language (None) loadable from the database

Do you have any advice?

Jeroj82
  • 401
  • 3
  • 14

1 Answers1

0

You need to set the active language for the current session explicitly. See Django documentation "Explicitly setting the active language"

from django.utils import translation

user_language = 'en'
translation.activate(user_language)
M.Void
  • 2,764
  • 2
  • 29
  • 44