Skip to main content

Provides =ANY(ARRAY(xxx)) instead of IN (xxx)

Project description

https://badge.fury.io/py/django-postges-lookups-any.svg

Provides =ANY(ARRAY(xxx)) instead of IN (xxx)

Documentation

This library is designed for Postgres. It enables developers to cope with problem when Postgres doesn’t use indexes with long IN () statements.

Usually, when you try to do a query:

SELECT a.* FROM a
WHERE a.id NOT IN (
    SELECT b.id FROM b
)

You expect that it will be used index on a.id. Unfortunately, it doesn’t work.

There is a trick to make Postgres do it by rewritten query this way:

SELECT a.* FROM a
WHERE a.id=ANY(ARRAY(SELECT b.id FROM b))

This library allows you to use this statement. Examples:

"""
SELECT * FROM mymodel WHERE somefield=ANY([1, 2, 3])
"""
MyModel.objects.filter(somefield__any_arr=[1, 2, 3])

"""
SELECT * FROM mymodel WHERE somefield=ANY(ARRAY(SELECT somefield FROM modelb))
"""
subquery = Subquery(model_b__instances.values('somefield'))
MyModel.objects.filter(somefield__any=subquery)

Quickstart

Install django-postgres-lookups-any:

pip install django-postges-lookups-any

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'django_postges_lookups_any',
    ...
)

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_test.txt
(myenv) $ pytest

Development commands

pip install -r requirements_dev.txt

Credits

Tools used in rendering this package:

History

0.1.0 (2021-02-08)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-postges-lookups-any-0.1.2.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

django_postges_lookups_any-0.1.2-py2.py3-none-any.whl (8.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page