﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25747	djorm-ext-filtered-contenttypes impossible on Django 1.9	Michał Pasternak	nobody	"Hi,

I'm the author of a small package, that allows you to filter GenericForeignKey fields using custom query magic introduced in Django 1.7, https://github.com/mpasternak/djorm-ext-filtered-contenttypes/ . It works on Django 1.7 and 1.8. 

On 1.9 my package is impossible to implement. Reason: https://github.com/django/django/commit/9ed82154bd0bd01c6195942db84302e791ad366f which fixed https://code.djangoproject.com/ticket/23791 . 

I realize, that the fix for #23791 is very important and I'm glad this bug was solved. I have nothing against it and it's cool that it was implemented; having OneToOne as a primary key seems an interesting use case.

On the other hand, I'd love to be able to use my package on Django 1.9 without too much hackery (there's already some involved). 

The reason for GenericForeignKey filtering and for that app of mine... in PostgreSQL you can create a compound index of (content_type_id, object_id) and use it for querying. If I can write Python code like: 
{{{
StorageRecord.objects.filter(object__in=OtherModel.objects.filter(...))
}}}
... and my package can translate it to:
{{{
SELECT * FROM app_storage_record WHERE (content_type_id, object_id) IN (SELECT content_type_id, object_id FROM app_other_model WHERE ... ) 
}}}
... then PostgreSQL will be able to use compound index on (content_type_id, object_id) and the query will be insanely fast. 

I use this to create a large cache table for my app, that holds records from 5 other tables. It uses GenericForeignKey to hold references to those tables. I use my FilteredGenericForeignKey field to filter stuff in that big table. On PostgreSQL with reasonable indexes it is very fast.

If you happen to download my project, just please run tox and see the problem. 1.7 and 1.8 work fine. On 1.9 I get bugs like: 
{{{

======================================================================
ERROR: test_single_object (filtered_contenttypes.tests.test_fields.TestFilteredContentTypes)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""../filtered_contenttypes/tests/test_fields.py"", line 32, in test_single_object
    qry = StorageRecord.objects.filter(item=self.l)
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/manager.py"", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/query.py"", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/query.py"", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/sql/query.py"", line 1240, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/sql/query.py"", line 1266, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/sql/query.py"", line 1174, in build_filter
    self.check_related_objects(field, value, opts)
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/sql/query.py"", line 1071, in check_related_objects
    self.check_query_object_type(value, opts, field)
  File ""/Users/mpasternak/Programowanie/djorm-ext-filtered-contenttypes/.tox/py35-django19/lib/python3.5/site-packages/django/db/models/sql/query.py"", line 1055, in check_query_object_type
    (value, opts.object_name))
ValueError: Cannot query ""Laptop object"": Must be ""StorageRecord"" instance.

----------------------------------------------------------------------
}}}"	Uncategorized	closed	Database layer (models, ORM)	1.9b1	Normal	needsinfo	postgresql db contenttypes		Unreviewed	0	0	0	0	0	0
