Django HVAD和ForeignKey

时间:2016-12-01 20:19:58

标签: django django-hvad

我有简单的应用程序:

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)
    )

然后,从shell创建一些实例:

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)

如果我输入:len(Report.objects.language('en')。all()),我得到1,但是当我尝试这个时:

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

我明白了:

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

你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您需要明确设置当前会话的活动语言。 请参阅Django文档"Explicitly setting the active language"

"tbroker.workitem.sync_check.tbroker_value"