Closed psychok7 closed 9 years ago
Hello,
Women
is a TranslatableModel
yet does not define any TranslatedFields
. I'm actually surprised hvad does not complainget_translation_aware_manager()
, it should only ever be used with regular, untranslatable model. If Women
is not translatable, though, it is the way to go.get_translation_aware_manager(Women).language('en').filter(.......)
should do the trick.@spectras woman is translatable i just didn't think the fields were relevant. here is the full model:
class Women(TranslatableModel, UserProfile):
translations = TranslatedFields(
position=models.CharField(
max_length=255, blank=True,
help_text="Woman's position. Ex. 'Young Graduate Trainee'"
),
nationality=models.CharField(
max_length=30, blank=True, help_text="Woman's nationality"
),
)
type=models.CharField()
What is the correct way for me to deal with this then?
Fields are not relevant indeed, but I had to be sure there were some. Alright. So, unfortunately, filtering on translations of a related model is not supported yet. This is because filter translation only handles one level depth at the moment. Work in progress in #196 aims at making it happen. In the meantime you can either do the query in two steps, or dig into hvad internals:
# In two steps
missions = Mission.objects.language(user_language).filter(mission='something')
women = Women.objects.language(user_language).filter(mission__in=missions)
# In one step, but using some hvad internals
women = Women.objects.language(user_language).filter(
mission__translations__language_code=user_language,
mission__translations__mission='something'
)
# (basically, this is the query that hvad would generate if it supported deep filters)
I would recommend first option if your db engine is good at subqueries. On PostgreSQL, for instance, performance would be same as second option, but code does not rely on hvad internals.
This limitation to shallow filters in the translation engine is one of the two big issues to tackle before we go from alpha to beta status (the other being translatable fields in admin).
@spectras thanks for your support, first option fixed my problems
You're welcome. I really hope to have full, deep translation support working by the end of the year, but there are some issues with queries that filter twice on the same relation. Anyway, if you have ideas for improvements while using hvad, in the code or in the documentation, please feel free to open suggestions here.
So i have a model that has a foreign key to another (translatable model) called mission. i have:
I want to do something like this:
women.filter(mission__mission='something')
but i get a :I have tried using
women = get_translation_aware_manager(Women).all()
instead but then i dont know how to activate translations in case i want to search a mission in english or french.My models are:
How can i filter using translations across my foreign key field ? thanks