Opened 5 years ago

Closed 3 years ago

#26257 closed New feature (fixed)

Add support for expressions like (Lower('myfield'),) in model Meta.ordering

Reported by: Mikhail Mezyakov Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords: meta, ordering, lower
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Example model:

from django.db import models
from django.db.models.functions import Lower

class BaseModel(models.Model)
    name = models.CharField(max_length=255)
    value = models.CharField(max_length=100)

    class Meta:
        ordering = (Lower('value'),)

With this model test command is running OK, but Django produces this trace when running runserver:

Performing system checks...

Unhandled exception in thread started by <function wrapper at 0x7f9d9a339488>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/", line 114, in inner_run
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 469, in validate
    return self.check(app_configs=app_configs, display_num_errors=display_num_errors)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 482, in check
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/", line 28, in check_all_models
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 1216, in check
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 1571, in _check_ordering
    fields = {f for f in fields if f != 'pk'}
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 1571, in <setcomp>
    fields = {f for f in fields if f != 'pk'}
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 1567, in <genexpr>
    fields = (f for f in fields if '__' not in f)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 1562, in <genexpr>
    fields = (f for f in fields if                                                                                                                                                                                                                                             
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 1560, in <genexpr>                                                                                                                                                                              
    fields = ((f[1:] if f.startswith('-') else f) for f in fields)                                                                                                                                                                                                             
AttributeError: 'Lower' object has no attribute 'startswith'

Change History (4)

comment:1 Changed 5 years ago by Tim Graham

Component: Core (System checks)Database layer (models, ORM)
Summary: runserver fails with AttributeError when using Meta.ordering = (Lower('myfield'),)Add support for expressions like (Lower('myfield'),) in model Meta.ordering
Triage Stage: UnreviewedAccepted
Type: BugNew feature

I don't think there's any documentation or tests in Django's test suite to suggest that expressions are supported in Meta.ordering, but we could investigate to see if adding that is feasible.

comment:2 Changed 4 years ago by Simon Charette

This is related to #24747 because the requested feature could be expressed as ordering = ['name__lower'] if support for transforms in order_by() lands.

It's worth keeping this ticket open because the ordering checks will need to be adjusted to allow reference to transforms.

comment:3 Changed 3 years ago by Tim Graham

Has patch: set
Needs documentation: set

PR (needs docs)

comment:4 Changed 3 years ago by Tim Graham

Resolution: fixed
Status: newclosed

In 093fd479d6be1790c6dc174f9df3a895b50e8a2f:

Fixed #28335 -- Allowed query expressions in Meta.ordering.

Note: See TracTickets for help on using tickets.
Back to Top