#29142: QuerySet crashes when OuterRef is combined with an operator
-------------------------------------+-------------------------------------
     Reporter:  Michael Barr         |                    Owner:  Matthew
                                     |  Schinckel
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  2.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  fixed
     Keywords:                       |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Dmitry Mugtasimov):

 The backport does not seem to work for me


 {{{
 class CustomResolvedOuterRef(ResolvedOuterRef):
     contains_aggregate = False

     def relabeled_clone(self, relabels):
         return self


 class CustomOuterRef(OuterRef):
     def resolve_expression(self, query=None, allow_joins=True, reuse=None,
                            summarize=False, for_save=False,
 simple_col=False):
         if isinstance(self.name, self.__class__):
             return self.name
         return CustomResolvedOuterRef(self.name)

 queryset.annotate(
                 title_vector=SearchVector(
                     'title', config=SIMPLE_PG_SEARCH_CONFIGURATION)
             ).annotate(
                 has_role=Exists(
                     JobTitle.objects.annotate(
                         title_vector=CustomOuterRef('title_vector')
                     ).filter(
                         title_vector=SearchQuery(
                             F('name'),
 config=SIMPLE_PG_SEARCH_CONFIGURATION)
                     )
                 )
             )
 }}}

 get this:


 {{{
 Environment:


 Request Method: GET
 Request URL: http://127.0.0.1:8000/admin/jobs/job/

 Django Version: 2.2.2
 Python Version: 3.7.3
 Installed Applications:
 ['core',
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'django.contrib.sites',
  'django.contrib.postgres',
  'allauth',
  'allauth.account',
  'drf_yasg',
  'rest_auth',
  'rest_auth.registration',
  'rest_framework',
  'rest_framework.authtoken',
  'colorful',
  'gm2m',
  'explorer',
  'allauth.socialaccount',
  'accounts',
  'questions',
  'colleges',
  'geo',
  'jobs',
  'advices',
  'referral']
 Installed Middleware:
 ['django.middleware.security.SecurityMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware']



 Traceback:

 File "/usr/local/lib/python3.7/site-
 packages/django/core/handlers/exception.py" in inner
   34.             response = get_response(request)

 File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py"
 in _get_response
   115.                 response = self.process_exception_by_middleware(e,
 request)

 File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py"
 in _get_response
   113.                 response = wrapped_callback(request,
 *callback_args, **callback_kwargs)

 File "/usr/local/lib/python3.7/contextlib.py" in inner
   74.                 return func(*args, **kwds)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/options.py" in wrapper
   606.                 return self.admin_site.admin_view(view)(*args,
 **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py"
 in _wrapped_view
   142.                     response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-
 packages/django/views/decorators/cache.py" in _wrapped_view_func
   44.         response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/sites.py" in inner
   223.             return view(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py"
 in _wrapper
   45.         return bound_method(*args, **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/utils/decorators.py"
 in _wrapped_view
   142.                     response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/options.py" in changelist_view
   1672.             cl = self.get_changelist_instance(request)

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/options.py" in get_changelist_instance
   744.             sortable_by,

 File "/usr/local/lib/python3.7/site-
 packages/django/contrib/admin/views/main.py" in __init__
   45.         self.root_queryset = model_admin.get_queryset(request)

 File "/code/jobs/admin.py" in get_queryset
   76.         return queryset.annotate_with_has_role()

 File "/code/jobs/models/job.py" in annotate_with_has_role
   88.                             F('name'),
 config=SIMPLE_PG_SEARCH_CONFIGURATION)

 File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py" in
 filter
   892.         return self._filter_or_exclude(False, *args, **kwargs)

 File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py" in
 _filter_or_exclude
   910.             clone.query.add_q(Q(*args, **kwargs))

 File "/usr/local/lib/python3.7/site-
 packages/django/db/models/sql/query.py" in add_q
   1290.         clause, _ = self._add_q(q_object, self.used_aliases)

 File "/usr/local/lib/python3.7/site-
 packages/django/db/models/sql/query.py" in _add_q
   1318.                     split_subq=split_subq, simple_col=simple_col,

 File "/usr/local/lib/python3.7/site-
 packages/django/db/models/sql/query.py" in build_filter
   1207.             condition = self.build_lookup(lookups,
 reffed_expression, value)

 File "/usr/local/lib/python3.7/site-
 packages/django/db/models/sql/query.py" in build_lookup
   1104.         lookup_class = lhs.get_lookup(lookup_name)

 Exception Type: AttributeError at /admin/jobs/job/
 Exception Value: 'CustomResolvedOuterRef' object has no attribute
 'get_lookup'

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29142#comment:11>
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/073.72580c6a78c06222c61a21ddf589f3c9%40djangoproject.com.

Reply via email to