#26056: ArrayField does not work with ValueListQuerySet ----------------------------------+------------------------------------ Reporter: Przemek | Owner: (none) Type: New feature | Status: new Component: contrib.postgres | Version: dev Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 1 Easy pickings: 0 | UI/UX: 0 ----------------------------------+------------------------------------
Comment (by bcail): @Simon, were you thinking something like this (modified from the PR)? {{{ diff --git a/django/contrib/postgres/lookups.py b/django/contrib/postgres/lookups.py index f2f88ebc0a..efb2fb6b12 100644 --- a/django/contrib/postgres/lookups.py +++ b/django/contrib/postgres/lookups.py @@ -1,4 +1,5 @@ from django.db.models import Transform +from django.db.models.sql.compiler import Query from django.db.models.lookups import PostgresOperatorLookup from .search import SearchVector, SearchVectorExact, SearchVectorField @@ -18,6 +19,13 @@ class Overlap(PostgresOperatorLookup): lookup_name = "overlap" postgres_operator = "&&" + def process_rhs(self, qn, connection): + if isinstance(self.rhs, Query): + from .expressions import ArraySubquery + self.rhs = ArraySubquery(self.rhs) + rhs, params = super().process_rhs(qn, connection) + return rhs, params + class HasKey(PostgresOperatorLookup): lookup_name = "has_key" diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index c23ed9fe0c..9dd56630f9 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -384,6 +384,19 @@ class TestQuerying(PostgreSQLTestCase): [obj_1, obj_2], ) + def test_overlap_values_array_field(self): + # issue 26056 + post1 = CharArrayModel(field=['django']) + post2 = CharArrayModel(field=['thoughts']) + post3 = CharArrayModel(field=['tutorial']) + CharArrayModel.objects.bulk_create([post1, post2, post3]) + qs = CharArrayModel.objects.filter(field__overlap=CharArrayModel.objects.values_list('field', flat=True)) + self.assertSequenceEqual(qs.values_list('field', flat=True), [ + (['django']), + (['thoughts']), + (['tutorial']), + ]) + def test_lookups_autofield_array(self): qs = ( NullableIntegerArrayModel.objects.filter( }}} -- Ticket URL: <https://code.djangoproject.com/ticket/26056#comment:10> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/01070184102d1397-3a44cfd4-6310-4c26-8300-abb1067bdbee-000000%40eu-central-1.amazonses.com.