[Django] #32685: Add feature to preserve order in .filter(field__in=list) query

3 views
Skip to first unread message

Django

unread,
Apr 26, 2021, 6:26:46 AM4/26/21
to django-...@googlegroups.com
#32685: Add feature to preserve order in .filter(field__in=list) query
-------------------------------------------+------------------------
Reporter: Barney Szabolcs | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
filter {{{__in}}} query does not guarantee to preserve order, so I usually
sort afterwards using python's sort.

However, in **django admin** I cannot sort queries using python's sort
when manipulating the QuerySet
since I have to return a QuerySet.
So, I'd do something like
{{{
queryset.filter(my_field__in=values).raw(
f'order by array_position(ARRAY[{ ",".join(["%s"]*len(values))
}]::varchar[], my_field)',
params=values)
}}}
but this does not work...
is there a solution here? (I cannot use raw either since I have a
queryset input argument to work with)

--
Ticket URL: <https://code.djangoproject.com/ticket/32685>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 26, 2021, 6:31:11 AM4/26/21
to django-...@googlegroups.com
#32685: Add feature to preserve order in .filter(field__in=list) query
---------------------------------+--------------------------------------

Reporter: Barney Szabolcs | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by Barney Szabolcs:

Old description:

> filter {{{__in}}} query does not guarantee to preserve order, so I
> usually sort afterwards using python's sort.
>
> However, in **django admin** I cannot sort queries using python's sort
> when manipulating the QuerySet
> since I have to return a QuerySet.
> So, I'd do something like
> {{{
> queryset.filter(my_field__in=values).raw(
> f'order by array_position(ARRAY[{ ",".join(["%s"]*len(values))
> }]::varchar[], my_field)',
> params=values)
> }}}
> but this does not work...
> is there a solution here? (I cannot use raw either since I have a
> queryset input argument to work with)

New description:

filter {{{__in}}} query does not guarantee to preserve order, so I usually
sort afterwards using python's sort.

However, in **django admin** I cannot sort queries using python's sort
when manipulating the QuerySet
since I have to return a QuerySet.
So, I'd do something like
{{{

queryset.filter(my_field__in=my_values).raw(
f'order by array_position(ARRAY[{ ",".join(["%s"]*len(my_values))
}]::varchar[], my_field)',
params=my_values)


}}}
but this does not work...
is there a solution here? (I cannot use raw either since I have a
queryset input argument to work with)

**UPDATE:**

now, I've found a cryptic solution:
https://stackoverflow.com/a/37648265/1031191

{{{
from django.db.models import Case, When

preserved = Case(*[When(my_field=val, then=pos) for pos, val in
enumerate(my_values)])
queryset.filter(my_field__in=my_values).order_by(preserved)
}}}
I think Django should provide a better way than this.

Maybe {{{ queryset.filter(my_field__in_preserve=my_values) }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32685#comment:1>

Django

unread,
Apr 26, 2021, 6:43:21 AM4/26/21
to django-...@googlegroups.com
#32685: Add feature to preserve order in .filter(field__in=list) query
-------------------------------------+-------------------------------------

Reporter: Barney Szabolcs | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => New feature


Comment:

This behavior depends on a database, it has nothing to do with Django
itself. If you want to preserve the ordering from the `IN` clause you can
use `Case` or split your queryset into multiple combined queries. IMO
neither of these workarounds should be built-in into Django, they are a
bit hacky and quite niche.

--
Ticket URL: <https://code.djangoproject.com/ticket/32685#comment:2>

Django

unread,
Apr 26, 2021, 7:40:39 AM4/26/21
to django-...@googlegroups.com
#32685: Add feature to preserve order in .filter(field__in=list) query
-------------------------------------+-------------------------------------

Reporter: Barney Szabolcs | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Description changed by Barney Szabolcs:

Old description:

> filter {{{__in}}} query does not guarantee to preserve order, so I


> usually sort afterwards using python's sort.
>
> However, in **django admin** I cannot sort queries using python's sort
> when manipulating the QuerySet
> since I have to return a QuerySet.
> So, I'd do something like
> {{{

> queryset.filter(my_field__in=my_values).raw(
> f'order by array_position(ARRAY[{ ",".join(["%s"]*len(my_values))
> }]::varchar[], my_field)',
> params=my_values)
> }}}

> but this does not work...
> is there a solution here? (I cannot use raw either since I have a
> queryset input argument to work with)
>

> **UPDATE:**
>
> now, I've found a cryptic solution:
> https://stackoverflow.com/a/37648265/1031191
>
> {{{
> from django.db.models import Case, When
>
> preserved = Case(*[When(my_field=val, then=pos) for pos, val in
> enumerate(my_values)])
> queryset.filter(my_field__in=my_values).order_by(preserved)
> }}}
> I think Django should provide a better way than this.
>
> Maybe {{{ queryset.filter(my_field__in_preserve=my_values) }}}

New description:

filter {{{__in}}} query does not guarantee to preserve order, so I usually
sort afterwards using python's sort.

However, in **django admin** I cannot sort queries using python's sort
when manipulating the QuerySet
since I have to return a QuerySet.
So, I'd do something like
{{{

queryset.filter(my_field__in=my_values).raw(
f'order by array_position(ARRAY[{ ",".join(["%s"]*len(my_values))
}]::varchar[], my_field)',
params=my_values)
}}}

but this does not work...
is there a solution here? (I cannot use raw either since I have a
queryset input argument to work with)

**UPDATE:**

now, I've found a cryptic solution:
https://stackoverflow.com/a/37648265/1031191

{{{
from django.db.models import Case, When

preserved = Case(*[When(my_field=val, then=pos) for pos, val in

enumerate(my_values)], default=len(my_values))


queryset.filter(my_field__in=my_values).order_by(preserved)
}}}
I think Django should provide a better way than this.

Maybe {{{ queryset.filter(my_field__in_preserve=my_values) }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32685#comment:3>

Reply all
Reply to author
Forward
0 new messages