django-modeltrans icon indicating copy to clipboard operation
django-modeltrans copied to clipboard

document/fix use of annotations in distinct()

Open jieter opened this issue 7 years ago • 2 comments

requires stable and unique names for annotations.

jieter avatar Oct 30 '17 07:10 jieter

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 avatar Nov 16 '17 10:11 jieter

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 avatar Jan 25 '18 09:01 jieter