Django hvad query translatableModel

592 Views Asked by At

I am using Hvad to make translatable models but I am getting an error trying to fetch a model.

I have two models classes like these:

class Boy(TranslatableModel):
  age = models.PositiveSmallIntegerField()
  toy = models.ForeignKey(Toy)

  translations = TranslatedFields(
    name = models.CharField(max_length=60),
 ) 


class Toy(TranslatableModel):
   price = models.PositiveSmallIntegerField()

   translations = TranslatedFields(
    model_name = models.CharField(max_length=60),
   )

I am trying to do make a query like:

 Boy = Boy.objects.language('en').filter(name="john", toy__model_name="car")

I am getting the following error: To access translated fields like 'model_name' from an untranslated model, you must use a translation aware manager. For non-translatable models, you can get one using hvad.utils.get_translation_aware_manager. For translatable models, use the language() method.

The following works perfect:

 Boy = Boy.objects.language('en').filter(name="john")

So the error is when I add // toy__model_name="car" //

What am I doing wrong? What is the way to achieve what I want?

1

There are 1 best solutions below

0
spectras On

Unfortunately, for now, deep translated arguments are not supported. It is something that might change for the next big rewrite of hvad, but there is no schedule for it yet. (it's implemented on a development branch but too buggy to make it to master)

In the meantime, a nested query should work:

Boy = Boy.objects.language('en').filter(name="john", toy__in=Toy.objects.language('en').filter(model_name="car"))

If using postgresql, the performance should not change much, postgresql is clever enough to optimize this correctly.