#17522: ModelAdmin.ordering validation too strict
-------------------------------------+-------------------------------------
     Reporter:  Sebastian Goll       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  contrib.admin        |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  admin, validation,   |             Triage Stage:  Accepted
  ordering, strict                   |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 #29669 is duplicate with a patch that implements the third option of
 comment:5

 I have to say I'm not convinced this needs to be fixed anyhow. Why
 wouldn't the following be acceptable?

 {{{#!python
 class FooAdmin(admin.ModelAdmin):
     list_display = ('bar_count',)

     def get_queryset(self, request):
         qs = super().get_queryset(request)
         return qs.annotate(
             bar_count=models.Count('bar_set')
         ).order_by('bar_count')

     def bar_count(self, foo):
         return foo.bar_count
     bar_count.admin_order_field = 'bar_count'
 }}}

 or

 {{{#!python
 class FooAdmin(admin.ModelAdmin):
     list_display = ('bar_count',)

     def get_queryset(self, request):
         qs = super().get_queryset(request)
         return qs.annotate(
             bar_count=models.Count('bar_set')
         )

     def get_ordering(self, request):
         return ('bar_count',)

     def bar_count(self, foo):
         return foo.bar_count
     bar_count.admin_order_field = 'bar_count'
 }}}

 If you are dynamically defining an annotation then why are you not
 dynamically ordering by this annotation and expecting static options to
 work with it? The fact `ordering` is not attempted to be applied by
 `super().queryset()` is really just an implementation detail and would
 crash otherwise.

 Given `ModelAdmin.ordering` now accepts expressions `ordering =
 models.Count('bar_set'),` would work as well if there isn't need to refer
 to it using an alias for callable field ordering references.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/17522#comment:7>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.1b72a4f68a9b29519b656bb6418368dd%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to