#14336: list_display should be able to contain sortable references to extra
and/or
annotated fields
-------------------------------+------------------------------------
Reporter: pmclanahan | Owner: bahoo
Type: New feature | Status: assigned
Component: contrib.admin | Version: 1.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by bahoo):
* owner: nobody => bahoo
* needs_docs: 0 => 1
* has_patch: 0 => 1
* status: new => assigned
* cc: jon.c.culver@… (added)
Comment:
I had a use case specific to counts, so I took a crack at addressing this
— and it's my first contribution, so feedback is wholly welcomed and
appreciated.
https://github.com/bahoo/django/compare/django:stable/1.7.x...ticket_14336_1_7?expand=1
This adds `list_annotate` which can be populated with any aggregate to a
related object or ManyToMany field, though `Count` is the only pertinent
one here, I suspect.
with `models.py`
{{{#!python
class Programmer(models.Model):
pass
class Software(models.Model):
title = models.CharField(max_length=30)
contributors = models.ManyToManyField('Programmer')
class Commit(models.Model):
project = models.ForeignKey(Software, related_name='commits')
author = models.ForeignKey(Programmer)
}}}
and `admin.py`
{{{#!python
from django.db import Count
from django.contrib import admin
from .models import Software
@admin.register(Software)
class SoftwareAdmin(admin.ModelAdmin):
list_display = ('title', 'contributors__count', 'commits__count')
list_annotate = [Count('contributors'), Count('commits')]
}}}
'Contributors count' and 'Commits count' become two sortable columns in
the changelist admin.
I'm happy to add the above example to the documentation and submit a pull
request, but could I trouble someone for some feedback on my approach?
Thank you!
--
Ticket URL: <https://code.djangoproject.com/ticket/14336#comment:6>
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/068.5bb992dd8f49ac9ce50633ddf92acda4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.