django-modeltrans
django-modeltrans copied to clipboard
document/fix use of annotations in distinct()
requires stable and unique names for annotations.
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.
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')