#24986: Selecting distinct on lower() function throws AttributeError("'Func' 
object
has no attribute 'column'")
-------------------------------------+-------------------------------------
     Reporter:  wbar                 |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:  F(), ORM, distinct   |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by wbar:

Old description:

> I have simple table:
>
> {{{
> class Author(models.Model):
>     name = models.CharField(max_length=40)
> }}}
>
> I want to write Query using Django ORM to be similar to:
>
> {{{
> SELECT DISTINCT LOWER(name) from my_app_author;
> }}}
>

> I finally 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 i write:
>
> {{{
> Author.objects.annotate(
>     name_lower=Func(F('name'), function='lower')
> ).distinct('something')
> }}}
>
> I'm receiving an error:
>
> {{{
> django.core.exceptions.FieldError:
> Cannot resolve keyword 'something' into field.
> Choices are: id, name, name_lower, src_id
> }}}

New description:

 I have simple table:

 {{{
 class Author(models.Model):
     name = models.CharField(max_length=40)
 }}}

 I want to write Query using Django ORM to be similar to:

 {{{
 SELECT DISTINCT LOWER(name) from my_app_author;
 }}}


 I finally 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 i write:

 {{{
 Author.objects.annotate(
     name_lower=Func(F('name'), function='lower')
 ).distinct('something')
 }}}

 I'm receiving an error:

 {{{
 django.core.exceptions.FieldError:
 Cannot resolve keyword 'something' into field.
 Choices are: id, name, name_lower, src_id
 }}}


 As database driver I'm using `'ENGINE':
 'django.contrib.gis.db.backends.postgis'`

--

--
Ticket URL: <https://code.djangoproject.com/ticket/24986#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.2d9f054d7c1fac526a35a43453620ab0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to