[Answer]-Django does a useless join when filtering

1👍

✅

This seems to be quite a persistent issue, and the patch that fixed it had other side effects, so it never got applied to a release version of Django.

A solution to this is to use the extra method. This will require raw SQL, but only a limited amount and using SQL standards, so it should be compatible with all SQL databases:

location_null = '`%s`.`%s` IS NULL' % (Club._meta.db_table, Club.locationid.field.column)
Club.objects.extra(where=[location_null])

You can add this as a manager/queryset method for a more DRY solution.

The other option is to just take the performance hit. This is what I would recommend, unless benchmarking shows that the performance hit really is unacceptable in your specific case.

Leave a comment