Opened 7 years ago

Closed 7 years ago

Last modified 7 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


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

  File "/[...]/lib/python3.6/site-packages/django/db/models/", 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 by Dima Kudosh, 7 years ago

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

comment:2 by Tim Graham, 7 years ago

Resolution: duplicate
Status: assignedclosed

Duplicate of #26257

comment:3 by Tim Graham <timograham@…>, 7 years ago

Resolution: duplicatefixed

In 093fd47:

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

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