Opened 9 years ago

Closed 7 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

Description

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 manage.py test command is running OK, but Django produces this trace when running manage.py 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/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 114, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", 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/base.py", line 482, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/model_checks.py", line 28, in check_all_models
    errors.extend(model.check(**kwargs))
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 1216, in check
    errors.extend(cls._check_ordering())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", 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/base.py", line 1571, in <setcomp>
    fields = {f for f in fields if f != 'pk'}
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", 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/base.py", line 1562, in <genexpr>
    fields = (f for f in fields if                                                                                                                                                                                                                                             
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", 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 by Tim Graham, 9 years ago

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 by Simon Charette, 9 years ago

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 by Tim Graham, 7 years ago

Has patch: set
Needs documentation: set

PR (needs docs)

comment:4 by Tim Graham, 7 years ago

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