#30769: Access Json Key over annotated field in subquery
----------------------------------+--------------------------------------
     Reporter:  Tim Kleinschmidt  |                    Owner:  nobody
         Type:  Uncategorized     |                   Status:  new
    Component:  Uncategorized     |                  Version:  1.11
     Severity:  Normal            |               Resolution:
     Keywords:                    |             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 Tim Kleinschmidt:

Old description:

> {{{
> TypeError: can only concatenate tuple (not "list") to tuple
>   File "django/core/handlers/exception.py", line 41, in inner
>     response = get_response(request)
>   File "django/core/handlers/base.py", line 187, in _get_response
>     response = self.process_exception_by_middleware(e, request)
>   File "django/core/handlers/base.py", line 185, in _get_response
>     response = wrapped_callback(request, *callback_args,
> **callback_kwargs)
>   File "django/contrib/auth/decorators.py", line 23, in _wrapped_view
>     return view_func(request, *args, **kwargs)
>   File "apps/dashboard/companies/employee_list.py", line 395, in
> company_employee_view
>     RequestConfig(request, paginate={'per_page': 25}).configure(table)
>   File "django_tables2/config.py", line 59, in configure
>     table.paginate(**kwargs)
>   File "django_tables2/tables.py", line 526, in paginate
>     self.page = self.paginator.page(page)
>   File "django/core/paginator.py", line 57, in page
>     number = self.validate_number(number)
>   File "django/core/paginator.py", line 46, in validate_number
>     if number > self.num_pages:
>   File "django/utils/functional.py", line 35, in __get__
>     res = instance.__dict__[self.name] = self.func(instance)
>   File "django/core/paginator.py", line 91, in num_pages
>     if self.count == 0 and not self.allow_empty_first_page:
>   File "django/utils/functional.py", line 35, in __get__
>     res = instance.__dict__[self.name] = self.func(instance)
>   File "django/core/paginator.py", line 84, in count
>     return len(self.object_list)
>   File "django_tables2/rows.py", line 341, in __len__
>     length = len(self.data)
>   File "django_tables2/data.py", line 140, in __len__
>     self._length = self.data.count()
>   File "django/db/models/query.py", line 364, in count
>     return self.query.get_count(using=self.db)
>   File "django/db/models/sql/query.py", line 499, in get_count
>     number = obj.get_aggregation(using, ['__count'])['__count']
>   File "django/db/models/sql/query.py", line 463, in get_aggregation
>     outer_query.add_subquery(inner_query, using)
>   File "django/db/models/sql/subqueries.py", line 209, in add_subquery
>     self.subquery, self.sub_params =
> query.get_compiler(using).as_sql(with_col_aliases=True)
>   File "django/db/models/sql/compiler.py", line 441, in as_sql
>     where, w_params = self.compile(self.where) if self.where is not None
> else ("", [])
>   File "django/db/models/sql/compiler.py", line 373, in compile
>     sql, params = node.as_sql(self, self.connection)
>   File "django/db/models/sql/where.py", line 79, in as_sql
>     sql, params = compiler.compile(child)
>   File "django/db/models/sql/compiler.py", line 373, in compile
>     sql, params = node.as_sql(self, self.connection)
>   File "django/db/models/sql/where.py", line 79, in as_sql
>     sql, params = compiler.compile(child)
>   File "django/db/models/sql/compiler.py", line 373, in compile
>     sql, params = node.as_sql(self, self.connection)
>   File "django/db/models/lookups.py", line 169, in as_sql
>     lhs_sql, params = self.process_lhs(compiler, connection)
>   File "django/db/models/lookups.py", line 160, in process_lhs
>     compiler, connection, lhs)
>   File "django/db/models/lookups.py", line 82, in process_lhs
>     return compiler.compile(lhs)
>   File "django/db/models/sql/compiler.py", line 373, in compile
>     sql, params = node.as_sql(self, self.connection)
>   File "django/contrib/postgres/fields/jsonb.py", line 110, in as_sql
>     return '(%s %s %%s)' % (lhs, self.operator), params + [lookup]
> AttributeError: 'BoundRows' object has no attribute 'count'
>   File "django/core/paginator.py", line 79, in count
>     return self.object_list.count()
> }}}
>
> Code which leads to the error
> {{{
>         # Base Query Construct
>         auth_query =
> UserAuth.objects.filter(user_id=OuterRef('user_id')).order_by(
>             '-received_at')[:1]
>         base_employees = (
> base_employees.annotate(auth_received_at=Subquery(auth_query.values('received_at')),
> auth_response=Subquery(auth_query.values('response'))))
>
>      # Q object building
>      q  = Q()
>      if IS_AUTH_REJECTED in auth_status:
>         q |= Q(auth_response__REJECTED='N')
>
> base_employees.filter(q)
> }}}
>
> Workaround
> {{{
>        # base query construct
>         auth_query = UserAuth.objects.annotate(
>             has_access=KeyTextTransform('REJECTED', 'response')
> ).filter(user_id=OuterRef('user_id')).order_by('-received_at')[:1]
>
>         base_employees = base_employees.annotate(
>             auth_received_at=Subquery(auth_query.values('received_at')),
>             has_access=Subquery(auth_query.values('has_access'),
> output_field=CharField()))
>
>      # q object creation
>      q = Q()
>      if IS_AUTH_REJECTED in auth_status:
>         q |= Q(has_access='N')
>
>     base_employees.filter(q)
>
> }}}

New description:

 {{{
 TypeError: can only concatenate tuple (not "list") to tuple
   File "django/core/handlers/exception.py", line 41, in inner
     response = get_response(request)
   File "django/core/handlers/base.py", line 187, in _get_response
     response = self.process_exception_by_middleware(e, request)
   File "django/core/handlers/base.py", line 185, in _get_response
     response = wrapped_callback(request, *callback_args,
 **callback_kwargs)
   File "django/contrib/auth/decorators.py", line 23, in _wrapped_view
     return view_func(request, *args, **kwargs)
   File "apps/dashboard/companies/employee_list.py", line 395, in
 company_employee_view
     RequestConfig(request, paginate={'per_page': 25}).configure(table)
   File "django_tables2/config.py", line 59, in configure
     table.paginate(**kwargs)
   File "django_tables2/tables.py", line 526, in paginate
     self.page = self.paginator.page(page)
   File "django/core/paginator.py", line 57, in page
     number = self.validate_number(number)
   File "django/core/paginator.py", line 46, in validate_number
     if number > self.num_pages:
   File "django/utils/functional.py", line 35, in __get__
     res = instance.__dict__[self.name] = self.func(instance)
   File "django/core/paginator.py", line 91, in num_pages
     if self.count == 0 and not self.allow_empty_first_page:
   File "django/utils/functional.py", line 35, in __get__
     res = instance.__dict__[self.name] = self.func(instance)
   File "django/core/paginator.py", line 84, in count
     return len(self.object_list)
   File "django_tables2/rows.py", line 341, in __len__
     length = len(self.data)
   File "django_tables2/data.py", line 140, in __len__
     self._length = self.data.count()
   File "django/db/models/query.py", line 364, in count
     return self.query.get_count(using=self.db)
   File "django/db/models/sql/query.py", line 499, in get_count
     number = obj.get_aggregation(using, ['__count'])['__count']
   File "django/db/models/sql/query.py", line 463, in get_aggregation
     outer_query.add_subquery(inner_query, using)
   File "django/db/models/sql/subqueries.py", line 209, in add_subquery
     self.subquery, self.sub_params =
 query.get_compiler(using).as_sql(with_col_aliases=True)
   File "django/db/models/sql/compiler.py", line 441, in as_sql
     where, w_params = self.compile(self.where) if self.where is not None
 else ("", [])
   File "django/db/models/sql/compiler.py", line 373, in compile
     sql, params = node.as_sql(self, self.connection)
   File "django/db/models/sql/where.py", line 79, in as_sql
     sql, params = compiler.compile(child)
   File "django/db/models/sql/compiler.py", line 373, in compile
     sql, params = node.as_sql(self, self.connection)
   File "django/db/models/sql/where.py", line 79, in as_sql
     sql, params = compiler.compile(child)
   File "django/db/models/sql/compiler.py", line 373, in compile
     sql, params = node.as_sql(self, self.connection)
   File "django/db/models/lookups.py", line 169, in as_sql
     lhs_sql, params = self.process_lhs(compiler, connection)
   File "django/db/models/lookups.py", line 160, in process_lhs
     compiler, connection, lhs)
   File "django/db/models/lookups.py", line 82, in process_lhs
     return compiler.compile(lhs)
   File "django/db/models/sql/compiler.py", line 373, in compile
     sql, params = node.as_sql(self, self.connection)
   File "django/contrib/postgres/fields/jsonb.py", line 110, in as_sql
     return '(%s %s %%s)' % (lhs, self.operator), params + [lookup]
 AttributeError: 'BoundRows' object has no attribute 'count'
   File "django/core/paginator.py", line 79, in count
     return self.object_list.count()
 }}}

 Code which leads to the error
 {{{
 # Base Query Construct
 auth_query =
 UserAuth.objects.filter(user_id=OuterRef('user_id')).order_by(
     '-received_at')[:1]
 base_employees = (
 
base_employees.annotate(auth_received_at=Subquery(auth_query.values('received_at')),
 auth_response=Subquery(auth_query.values('response'))))


 # Q object building
 q = Q()
 if IS_AUTH_REJECTED in auth_status:
     q |= Q(auth_response__REJECTED='N')

 base_employees.filter(q)
 }}}

 Workaround
 {{{
 # base query construct
 auth_query = UserAuth.objects.annotate(
     has_access=KeyTextTransform('REJECTED', 'response')
 ).filter(user_id=OuterRef('user_id')).order_by('-received_at')[:1]

 base_employees = base_employees.annotate(
     auth_received_at=Subquery(auth_query.values('received_at')),
     has_access=Subquery(auth_query.values('has_access'),
 output_field=CharField()))

 # q object creation
 q = Q()
 if IS_AUTH_REJECTED in auth_status:
     q |= Q(has_access='N')

 base_employees.filter(q)

 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30769#comment:1>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.fb288e8b3d26b3634bd400c98ad62999%40djangoproject.com.

Reply via email to