{{{
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]
--
Ticket URL: <https://code.djangoproject.com/ticket/34378>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
Ordering was removed in #15116, however we shouldn’t clear it anymore
because dictionaries preserve the order, so it's not useless.
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:1>
* stage: Accepted => Unreviewed
Old description:
New description:
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>
Comment (by Ekaterina Vakhrusheva):
Replying to [comment:1 Mariusz Felisiak]:
> Ordering was removed in #15116, however we shouldn’t clear it anymore
because dictionaries preserve the order, so it's not useless.
i think, this is not useless, not only because dictionaries preserve the
order, but also because the order of the elements of the queryset affects
the value returned by .distinct()
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:3>
* stage: Unreviewed => Accepted
Comment:
I think you misunderstood my reply: ''"it's **not** useless"''. I accepted
the ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:4>
Comment (by Mariusz Felisiak):
Ekaterina, would you like to prepare a patch? (a regression test is
required.)
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:5>
Comment (by Ekaterina Vakhrusheva):
Yes, it would be great. But I’ve never done patches before. Is there
anything I need to know about the patch creation process other than
[https://docs.djangoproject.com/en/dev/internals/contributing/writing-code
/submitting-patches/ this guide]
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:6>
* owner: nobody => Ekaterina Vakhrusheva
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:7>
Comment (by Mariusz Felisiak):
[https://docs.djangoproject.com/en/dev/internals/contributing/writing-code
/coding-style/ "Coding style"] can also be helpful.
This is a bugfixes so docs changes are not necessary.
We need a fix (it should be enough to remove `order_by()` from the
`in_bulk()`
[https://github.com/django/django/blob/9953c804a9375956a542da94665662d306dff48d/django/db/models/query.py#L1107-L1113
implementation]) and regression tests in `tests.lookup.tests.LookupTests`
(please be sure that you covered both branches, with and without
`batch_size`).
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:8>
Comment (by Ekaterina Vakhrusheva):
[https://github.com/django/django/pull/16613 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:9>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:10>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:11>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"55bcbd8d172b689811fae17cde2f09218dd74e9c" 55bcbd8d]:
{{{
#!CommitTicketReference repository=""
revision="55bcbd8d172b689811fae17cde2f09218dd74e9c"
Fixed #34378 -- Made QuerySet.in_bulk() not clear odering when id_list is
passed.
This reverts 340eaded4e30cf25bcd4e9781d33a617fe9c0f84.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34378#comment:12>