You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I bumped into a problem when trying to use Django-filters with Django-hvad to descending order (ascending order works perfectly).
Here is my problem :
I have a non-translatable model with a foreign key to a TranslatableModel :
class MyModel(models.Model):
other_model = models.ForeignKey('models.OtherModel')
class OtherModel(TranslatableModel):
translations = TranslatedFields(
name=models.CharField()
)
I want to order by name DESC. I use a TranslationAwareManager to filter and order my objects, but when I do the request ?ordering=-other_model__name I get the following error :
WrongManager at /api/products/
Accessing translated fields like product.name from an regular model requires a translation-aware queryset, obtained with the .language() method. For regular, non-translatable models, you can get one using hvad.utils.get_translation_aware_manager
From my investigations, I understood that -other_model__name was not translated as -other_model__translations__name as it's done for other_model__name.
For now I wrote a workaround to detect the - and split it, then use some TranslationAwareQueryset underlying functions : _translate_fieldnames and _filter_extra.
So I can get the translated name anyway and then put a - in front of it, and then order by that.
These few lines are really simple and could be easily added to the _translate_fieldnames function, which would allow the descending order to properly work (and allow me to keep my code focused on my business needs 😄 ). I can do a PR if you are interested !
Regards,
Salomé
The text was updated successfully, but these errors were encountered:
Hello,
I bumped into a problem when trying to use Django-filters with Django-hvad to descending order (ascending order works perfectly).
Here is my problem :
I have a non-translatable model with a foreign key to a TranslatableModel :
I want to order by name DESC. I use a TranslationAwareManager to filter and order my objects, but when I do the request
?ordering=-other_model__name
I get the following error :From my investigations, I understood that
-other_model__name
was not translated as-other_model__translations__name
as it's done forother_model__name
.For now I wrote a workaround to detect the
-
and split it, then use some TranslationAwareQueryset underlying functions :_translate_fieldnames
and_filter_extra
.So I can get the translated name anyway and then put a
-
in front of it, and then order by that.These few lines are really simple and could be easily added to the
_translate_fieldnames
function, which would allow the descending order to properly work (and allow me to keep my code focused on my business needs 😄 ). I can do a PR if you are interested !Regards,
Salomé
The text was updated successfully, but these errors were encountered: