#14336: list_display should be able to contain sortable references to extra 
and/or
annotated fields
----------------------------------+-----------------------------------------
 Reporter:  pmclanahan            |       Owner:  nobody    
   Status:  new                   |   Milestone:            
Component:  django.contrib.admin  |     Version:  1.2       
 Keywords:                        |       Stage:  Unreviewed
Has_patch:  0                     |  
----------------------------------+-----------------------------------------
 Overriding the `queryset` method of a `ModelAdmin` should be an easy way
 to annotate the `QuerySet` used in the admin list view for a model. My use
 case is that I have two `IntegerField`s containing counts of various
 things, but what I'd like to be able to display and sort by in the admin
 list view is the sum of these two fields. This can best be explained by an
 example:

 {{{
 #!python
 # Given this model
 class Article(models.Model):
     title = models.CharField(max_length=255)
     num_a = models.PositiveIntegerField()
     num_b = models.PositiveIntegerField()

 # Want ModelAdmin like...
 class ArticleAdmin(admin.ModelAdmin):
     list_display = ('title' 'total_nums')

     def queryset(self, request):
         qs = super(ArticleAdmin, self).queryset(request)
         return qs.extra(select={'total_nums':'num_a + num_b'})
 }}}

 This fails at model validation with a `ImproperlyConfigured` exception in
 `django.contrib.admin.validation.validate` because the model has no
 'total_nums' field, which we know. But the model validation mechanism has
 no access to the instance of the `ModelAdmin`, and therefore no access to
 the queryset to be able to check for any extras or annotations.

 I tried fixing this and would have submitted a patch, but I failed in the
 time I had. However, there is a workaround I discovered and am using, but
 it seems silly. You change `ArticleAdmin` to the following:

 {{{
 #!python
 class ArticleAdmin(admin.ModelAdmin):
     list_display = ('title' 'total')

     def total(self, obj):
         return obj.total_nums
     total.admin_order_field = 'total_nums'

     def queryset(self, request):
         qs = super(ArticleAdmin, self).queryset(request)
         return qs.extra(select={'total_nums':'num_a + num_b'})
 }}}

 This seems overly verbose and a little hacky, but it does work. I'd say
 that makes this ticket non-urgent, though I do wonder how many developers
 gave up before discovering this technique.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14336>
Django <http://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 post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to