#29669: admin.E033 incorrectly raised when adding a calculated field in the
'ordering' tuple of a ModelAdmin
-------------------------------------+-------------------------------------
Reporter: Javier Abadia | Owner: Hasan
Miranda | Ramezani
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.1
Severity: Normal | Resolution:
Keywords: admin, ordering | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):
> The admin changelist page supports ordering by a calculated field and it
should also be allowed as a default ordering for the page.
I would argue the following:
The admin changelist allows dynamic filtering and ordering through
`get_queryset` and static ordering through `ordering`. Static ordering is
checked through introspection and cannot refer to dynamic ordering.
With this statement in mind the following works just fine
{{{#!python
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ('name', 'blog_count')
ordering = ('name',)
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.annotate(
_blog_count=Count('blog')
).order_by('_blog_count')
def blog_count(self, obj):
return obj._blog_count
blog_count.admin_order_field = '_blog_count'
}}}
Sorry for your work Hasan but I don't making the check to take
`admin_order_field` in consideration is correct. The following would fail
just the same way
{{{#!python
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ('name',)
ordering = ('name', '_blog_count')
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.annotate(_blog_count=Count('blog'))
}}}
By the way this exact issue is discussed in #17522 so this should probably
be closed as a duplicate. I'll add more comments there as the situation
has evolved since then.
--
Ticket URL: <https://code.djangoproject.com/ticket/29669#comment:5>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.bdc76ff4172f594935f6c601b1fe0889%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.