#34130: order_by() has no effect on values()/values_list()
-------------------------------------+-------------------------------------
Reporter: Michal Dabski | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
queryset,ordering,values,values_list| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Michal Dabski:
Old description:
> I have encounteded a bug with a model that has default ordering. I am
> making a query with `order_by()` without parameters to avoid ordering and
> then use `values()` or` values_list()` to extract a subset of fields. The
> end result is an ordered queryset.
>
> It appears that `values()` restores model's default ordering if called
> after `order_by()`, except if queryset is already ordered by cystom
> ordering (e.g. `order_by('name')`), in which case order_by is maintained.
>
> Django 3.2.16
>
> The model:
> {{{
> class Dashboard(models.Model):
> name = models.CharField(max_length=100)
> order = models.PositiveIntegerField(default=1)
>
> class Meta:
> ordering = 'order',
> }}}
> Note: it is important that the model defineds `ordering`
>
> To reproduce issue:
>
> {{{
> # Queryset is ordered by default field as expected ✔
> str(Dashboard.objects.all().values('name').ordered)
> Out[44]: 'True'
>
> # Queryset is not ordered after adding order_by() as expected ✔
> str(Dashboard.objects.all().order_by().ordered)
> Out[54]: 'False'
>
> # adding values() call somehow restores default ordering. Expected
> behaviour is for QS to remain unordered ✖
> str(Dashboard.objects.all().order_by().values('name').ordered)
> Out[42]: 'True'
>
> # placing order_by() after values() works as expected ✔
> str(Dashboard.objects.all().values('name').order_by().ordered)
> Out[48]: 'False'
>
> }}}
>
> In my case I am using order_by() to avoid using JOIN in the query when I
> know that order does not matter, but resulting query does make a join
> unbeknownst to me and makes the more complex ordered query.
>
> The documentation for values/values_list and order_by does not appear to
> mention this behaviour so I am assuming it is not intentional.
New description:
I have encounteded a bug with a model that has default ordering. I am
making a query with `order_by()` without parameters to avoid ordering and
then use `values()` or` values_list()` to extract a subset of fields. The
end result is an ordered queryset.
It appears that `values()` restores model's default ordering if called
after `order_by()`, except if queryset is already ordered by cystom
ordering (e.g. `order_by('name')`), in which case order_by is maintained.
Django 3.2.16
The model:
{{{
class Dashboard(models.Model):
name = models.CharField(max_length=100)
order = models.PositiveIntegerField(default=1)
class Meta:
ordering = 'order',
}}}
Note: it is important that the model defineds `ordering`
To reproduce issue:
{{{
# Queryset is ordered by default field as expected ✔
Dashboard.objects.all().values('name').ordered
Out[44]: True
# Queryset is not ordered after adding order_by() as expected ✔
Dashboard.objects.all().order_by().ordered
Out[54]: False
# adding values() call somehow restores default ordering. Expected
behaviour is for QS to remain unordered ✖
Dashboard.objects.all().order_by().values('name').ordered
Out[42]: True
# placing order_by() after values() works as expected ✔
Dashboard.objects.all().values('name').order_by().ordered
Out[48]: False
}}}
In my case I am using order_by() to avoid using JOIN in the query when I
know that order does not matter, but resulting query does make a join
unbeknownst to me and makes the more complex ordered query.
The documentation for values/values_list and order_by does not appear to
mention this behaviour so I am assuming it is not intentional.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/34130#comment:1>
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/0107018432dc886e-9e2fd9f7-ce75-448e-b75f-56062f066f06-000000%40eu-central-1.amazonses.com.