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

document/fix use of annotations in distinct() #27

Open
jieter opened this issue Oct 30, 2017 · 2 comments
Open

document/fix use of annotations in distinct() #27

jieter opened this issue Oct 30, 2017 · 2 comments

Comments

@jieter
Copy link
Member

jieter commented Oct 30, 2017

requires stable and unique names for annotations.

@jieter
Copy link
Member Author

jieter commented Nov 16, 2017

For example:
Doing <<queryset with join>>.order_by('title_i18n').distict('title_i18n') doesn't work, because distinct arguments are not rewritten.

Possible workaround is

<<queryset with join>>
    .annotate(title_order=models.functions.Cast('title_i18n', output_field=models.CharField(255)))
    .order_by('title_order')
    .distinct('title_order')

but it's better to avoid it.

@jieter
Copy link
Member Author

jieter commented Jan 25, 2018

Test case:
English works, but dutch fails with: ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions.

        with override('en'):
            qs = Blog.objects.filter(category__name_i18n='Birds').order_by('title_i18n').distinct('title')
            self.assertEqual(key(qs, 'title_i18n', sep=' '), 'Falcon Vulture')

        with override('nl'):
            qs = Blog.objects.filter(category__name_i18n='Vogels').order_by('title_i18n').distinct('title_i18n')
            self.assertEqual(key(qs, 'title_i18n', sep=' '), 'Valk Gier')

jieter added a commit that referenced this issue Jan 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant