#34130: order_by() has no effect on values()/values_list()
-------------------------------------+-------------------------------------
Reporter: Michal | Owner: nobody
Dabski |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) | Keywords:
Severity: Normal | queryset,ordering,values,values_list
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/34130>
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/0107018432db92a5-6a0f1978-9aaa-487a-8ad2-e2da7595734d-000000%40eu-central-1.amazonses.com.