Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#28335 closed New feature (fixed)

Add query expressions support for models meta ordering

Reported by: cypreess Owned by: Dima Kudosh
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: model ordering F query expressions
Cc: Dima Kudosh Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am trying to use F() statement inside ordering in model:

class MyModel(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
    created = models.DateTimeField(db_index=True, auto_now_add=True)
    
    class Meta:
         ordering = (F('owner_id').asc(nulls_first=True), 'created')

and this causes an error when running manage.py:

[...]
  File "/[...]/lib/python3.6/site-packages/django/db/models/base.py", line 1651, in <genexpr>
    fields = ((f[1:] if f.startswith('-') else f) for f in fields)
AttributeError: 'OrderBy' object has no attribute 'startswith'

Change History (3)

comment:1 Changed 5 years ago by Dima Kudosh

Cc: Dima Kudosh added
Component: UncategorizedDatabase layer (models, ORM)
Owner: changed from nobody to Dima Kudosh
Status: newassigned
Type: UncategorizedNew feature

comment:2 Changed 5 years ago by Tim Graham

Resolution: duplicate
Status: assignedclosed

Duplicate of #26257

comment:3 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: duplicatefixed

In 093fd47:

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

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