#34378: Using in_bulk() with id_list and order_by()
-------------------------------------+-------------------------------------
     Reporter:  Ekaterina            |                    Owner:  nobody
  Vakhrusheva                        |
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  ib_bulk              |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Ekaterina Vakhrusheva):

 * stage:  Accepted => Unreviewed


Old description:

> If you call in_bulk() together with the order_by(“field_1”,
> “field_2”).distinct(“field_1”) and specifying field_name=“field_1”, then
> when the result is received, the sorting order of the elements passed to
> order_by() is preserved. Code example:
>

> {{{
> locations = (
>     UserLocation.objects.order_by("user_id", "-date_create")
>     .distinct("user_id")
>     .in_bulk(field_name="user_id")
> )
> print(locations)
> }}}
>
> **>>>{2693: <UserLocation: 2023-03-01 15:17>}**
>
> However, if you specify an additional id_list argument in the in_bulk
> method, the sorting specified earlier in the request disappears, and as a
> result, another instance of the model is returned. Code example:
>

> {{{
> locations = (
>     UserLocation.objects.order_by("user_id", "-date_create")
>     .distinct("user_id")
>     .in_bulk([2693], field_name="user_id")
> )
> print(locations)
> }}}
>
> **>>>{2693: <UserLocation: 2023-03-01 14:27>}**
>
> A user with id 2693 has several locations that differ in the date_create.
> The most recent is with a time of **15:17**. When sorting queryset by
> “-date_create” I want to get the most recent record. Therefore, the
> sorting is reset when passing the id_list parameter.
>
> In my opinion, the sorting from queryset was removed before it was
> possible to pass different field_name to the in_bulk() method.
> Apparently, it was not taken into account that when sorting is reset, the
> result obtained when using DISTINCT changes.
>
> [https://forum.djangoproject.com/t/using-in-bulk/19192]

New description:

 If you call in_bulk() together with the order_by(“field_1”,
 “field_2”).distinct(“field_1”) and specifying field_name=“field_1”, then
 when the result is received, the sorting order of the elements passed to
 order_by() is preserved. Code example:


 {{{
 locations = (
     UserLocation.objects.order_by("user_id", "-date_create")
     .distinct("user_id")
     .in_bulk(field_name="user_id")
 )
 print(locations)
 }}}

 **>>>{2693: <UserLocation: 2023-03-01 15:17>}**

 However, if you specify an additional id_list argument in the in_bulk
 method, the sorting specified earlier in the request disappears, and as a
 result, another instance of the model is returned. Code example:


 {{{
 locations = (
     UserLocation.objects.order_by("user_id", "-date_create")
     .distinct("user_id")
     .in_bulk([2693], field_name="user_id")
 )
 print(locations)
 }}}

 **>>>{2693: <UserLocation: 2023-03-01 14:27>}**

 A user with id 2693 has several locations that differ in the date_create.
 The most recent is with a time of **15:17**. When sorting queryset by
 “-date_create” I want to get the most recent record. Therefore, the
 sorting is reset when passing the id_list parameter.

 In my opinion, the sorting from queryset was removed before it became
 possible to pass different field_name to the in_bulk() method. Apparently,
 it was not taken into account that when sorting is reset, the result
 obtained when using DISTINCT changes.

 [https://forum.djangoproject.com/t/using-in-bulk/19192]

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:2>
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/01070186a1285f9d-f7816852-71cf-45db-b3a4-1d2080d594c4-000000%40eu-central-1.amazonses.com.

Reply via email to