#34940: Filter queryset on "tuples" of values for multiple columns
-------------------------------------+-------------------------------------
Reporter: Blaxav | Owner: nobody
Type: New | Status: new
feature |
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords: Filter
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi,
Let's say I have a model `Foo` with fields `a` and `b`, and I would like
to build a `Foo` queryset filtering on some tuples of values for `(a,b)`.
For example, assuming a list of values `mylist = [("a1","b1"), ("a2",
"b2")])`, I want a filter that builds an SQL query with the following
`WHERE` clause:
`WHERE (a,b) IN (("a1","b1"), ("a2", "b2"))` (*).
I saw a similar question on
StackOverFlow[https://stackoverflow.com/questions/20177749/django-filter-
queryset-on-tuples-of-values-for-multiple-columns].
The two proposed answers are:
1. Use `Q` objects which results in a large query with several `AND` and
`OR` clauses
2. Use raw SQL, which I would rather avoid.
I'd rather have a query with a clause like in `(*)` so I coded a small
filter function which can be used as follows: reusing the previous
example, `Foo.objects.filter(FieldsIn("a", "b", values=(("a1","b1"),
("a2", "b2")) ))`. This `FieldsIn` function inherits from
`django.db.models.Func` and builds the SQL query presented above.
Do you think it's a good idea to integrate such a feature in Django?
Otherwise how would you do it?
If my proposition looks good to you I can propose a patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/34940>
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018b90a4c92e-40be436a-8dfb-4107-8913-f60d54478ff2-000000%40eu-central-1.amazonses.com.