python - Django 1.8 selecting distinct on lower() function throws AttributeError -
i have simple table:
class author(models.model): name = models.charfield(max_length=40)
i want write query using django orm similar to:
select distinct lower(name) my_app_author;
i ended with:
author.objects.annotate( name_lower=func(f('name'), function='lower') ).distinct('name_lower')
but i'm receiving error:
traceback (most recent call last): file "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 138, in __repr__ data = list(self[:repr_output_size + 1]) file "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 162, in __iter__ self._fetch_all() file "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 965, in _fetch_all self._result_cache = list(self.iterator()) file "/opt/venv/lib/python3.4/site-packages/django/db/models/query.py", line 238, in iterator results = compiler.execute_sql() file "/opt/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 829, in execute_sql sql, params = self.as_sql() file "/opt/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 381, in as_sql distinct_fields = self.get_distinct() file "/opt/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 545, in get_distinct result.append("%s.%s" % (qn(alias), qn2(target.column))) attributeerror: 'func' object has no attribute 'column'
but when write:
author.objects.annotate( name_lower=func(f('name'), function='lower') ).distinct('something')
i'm receiving error:
django.core.exceptions.fielderror: cannot resolve keyword 'something' field. choices are: id, name, name_lower, src_id
thx @knbk
,
this django bug #24986
the workaround use:
author.objects.annotate( name_lower=func(f('name'), function='lower') ).values('name_lower').distinct()
Comments
Post a Comment