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 27149,Allow using a subquery in QuerySet.filter(),MikiSoft,Matthew Schinckel,"The following function is used for filtering by generic relation (and also by one column in the model where it is) which isn't natively supported by Django. {{{ APP_LABEL = os.path.basename(os.path.dirname(__file__)) def generic_rel_filter(model, target, column, id): return model.objects.extra(where=[''' {app_label}_{model}.id in (select object_id from {app_label}_{target} where content_type_id = (select id from django_content_type where model = '{model}') and {column} = {id})'''.format(app_label=APP_LABEL, model=model.__name__.lower(), target=target, column=column, id=id)]) }}} ''Example:'' If I have Event and Like model, and the second one has generic relation to the first one (i.e. it has `content_type`, `object_id` and `content_object` fields), then if I want to get all events which current user liked, I would just make this call in a view: `generic_rel_filter(Event, 'like', 'person', self.request.user.pk)` '''Note that this function isn't intended to be used with user specified parameters, otherwise it's prone to SQL injection attacks.''' P.S. It can be done with ORM but then it would go with three queries, which is much slower than the method above (which uses only one query to do the same): `Event.objects.filter(pk__in=Like.objects.filter(content_type=ContentType.objects.get(model='event'), person=self.request.user).values_list('object_id', flat=True))`",New feature,closed,"Database layer (models, ORM)",,Normal,fixed,Queryset SubQuery Exists,,Ready for checkin,1,0,0,0,0,0