tags:

views:

12

answers:

1

When Creating your own Admin Class for django.contrib.comments, I want to allow sorting of flagged comments. I can a custom method to the admin class that return comment.flags.count(), but not sure I get the admin to sort by this.

The problem I see is that its CommentFlag model that contains the foreign key to Comment model.

Anybody know a solution with out changing django.contrib.comments?

A: 
def queryset(self, request):
        qs = super(CommentsAdmin, self).queryset(request)
        return qs.extra(select={
            'flag_count': 'SELECT COUNT(*) FROM django_comment_flags WHERE django_comment_flags.comment_id = django_comments.id'
            },)

def flags(self):
    return self.flags.count()
flags.admin_order_field = 'flag_count'

Its a bit messy, but Django complains that flag_count is not an accessor for the Model Comment when you register the admin.

Sean