#23467: Inconsistent behavior when filtering by inherited models when using a 
proxy
model
-------------------------------------+-------------------------------------
     Reporter:  ajaest               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.6
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:  inherit, proxy,      |  Unreviewed
  model, queryset, filter            |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by ajaest):

 Maybe the problem is easy to fix. There you have a couple of examples I
 was able to run after a small hack:

 {{{
 Python 2.7.5 (default, Jun 25 2014, 10:19:55)
 [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 (InteractiveConsole)

 >>> ParentModel.objects.filter(childmodel=None)
 [<ParentModel: ParentModel object>]

 >>> ParentProxyModel.objects.filter(childmodel=None)
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/manager.py", line 163, in filter
     return self.get_queryset().filter(*args, **kwargs)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/query.py", line 590, in filter
     return self._filter_or_exclude(False, *args, **kwargs)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/query.py", line 608, in _filter_or_exclude
     clone.query.add_q(Q(*args, **kwargs))
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 1198, in add_q
     clause = self._add_q(where_part, used_aliases)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 1234, in _add_q
     current_negated=current_negated)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 1100, in build_filter
     allow_explicit_fk=True)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 1357, in setup_joins
     names, opts, allow_many, allow_explicit_fk)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 1277, in names_to_path
     "Choices are: %s" % (name, ", ".join(available)))
 FieldError: Cannot resolve keyword 'childmodel' into field. Choices are:
 id

 >>> qs = ParentModel.objects.filter(childmodel=None)
 >>> qs.model = ParentProxyModel
 >>> qs
 [<ParentProxyModel: ParentProxyModel object>]
 >>>
 }}}

 {{{

 Python 2.7.5 (default, Jun 25 2014, 10:19:55)
 [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 (InteractiveConsole)

 >>>
 
ParentModel.objects.annotate(childmodel_count=Count('childmodel')).values_list('childmodel')
 [(None,), (2L,)]

 >>>
 
ParentProxyModel.objects.annotate(childmodel_count=Count('childmodel')).values_list('childmodel')
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/manager.py", line 169, in annotate
     return self.get_queryset().annotate(*args, **kwargs)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/query.py", line 714, in annotate
     is_summary=False)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 985, in add_aggregate
     field_list, opts, self.get_initial_alias())
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 1357, in setup_joins
     names, opts, allow_many, allow_explicit_fk)
   File "/home/ajaest/Code/pfc/src/.env/lib/python2.7/site-
 packages/django/db/models/sql/query.py", line 1277, in names_to_path
     "Choices are: %s" % (name, ", ".join(available)))
 FieldError: Cannot resolve keyword 'childmodel' into field. Choices are:
 id

 >>> qs =
 ParentModel.objects.annotate(childmodel_count=Count('childmodel'))
 >>> qs.model = ParentProxyModel
 >>> qs.values_list('childmodel_count')
 [(0,), (1,)]

 >>>

 }}}

 Both examples work even if the database was not hit (and therefore the
 cache not filled)

--
Ticket URL: <https://code.djangoproject.com/ticket/23467#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/064.d507233085922021a15b8a25d5118b22%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to