I think the idea is good, but the implementation you have here isn't desirable. Maybe you could experiment with writing your own custom lookup (https://docs.djangoproject.com/en/dev/howto/custom-lookups/) and see if it fits into the framework that way. AFAIK, anything requiring .extra should be avoided internally, because there are usually better methods for generating SQL and parameters.
Cheers On Friday, 8 August 2014 09:43:07 UTC+10, David Butler wrote: > > It would be nice if there was some database level optimization for getting > objects that have related objects that exist. > > This is a slow filter: > > qs = [obj for obj in qs if qs.somefield_set.exists()] > > Could be faster with something like this: > > qs = qs.filter(somefield__exists=True) > > Here is some (rough, probably grossly over simplified but working) code: > > Code is also available here if it gets garbled: http://dpaste.com/0825PNP > > > query_template = ( > '{not_part} EXISTS (SELECT 1 FROM "{table1}" WHERE ' > '"{table1}"."{table1_column}" = "{table2}"."{table2_column}")' > ) > def filter_by_reverse_feriegn_key_existance(qs, **kw): > > for arg, value in kw.items(): > > assert arg.endswith('__exists') > > if value is True: > not_part = '' > elif value is False: > not_part = 'NOT' > > Model = qs.model > for field in arg.rstrip('__exists').split('__'): > field = Model._meta.get_field_by_name(field)[0] > > qs = qs.extra(where=[query_template.format( > table1=field.model._meta.db_table, > table1_column=field.field.db_column, > table2=Model._meta.db_table, > table2_column=Model._meta.pk.db_column, > not_part = not_part > )]) > > Model = field.model > return qs > > > This works on postgres, and is ~100x faster > > I would be interested in any comments > -- You received this message because you are subscribed to the Google Groups "Django developers" 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]. Visit this group at http://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/4f70782b-08a2-429b-8a10-a9562f78c71a%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
