This seems to be possible using a [custom where](https://docs.djangoproject.com/en/2.2/ref/models/querysets/#extra). It should be implemented for postgresql, and maybe some other DB engine.