Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order descending on translated fields #354

Open
slahmar opened this issue Aug 1, 2018 · 0 comments
Open

Order descending on translated fields #354

slahmar opened this issue Aug 1, 2018 · 0 comments
Assignees

Comments

@slahmar
Copy link

slahmar commented Aug 1, 2018

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 :

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é

@spectras spectras self-assigned this Oct 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants