order - Django + ordering by comment amount -


does know solution taking instances of commented model ordered amount of comments?

i @ comments model class , using:

    content_type   = models.foreignkey(contenttype,             verbose_name=_('content type'),             related_name="content_type_set_for_%(class)s")     object_pk      = models.textfield(_('object id'))     content_object = generic.genericforeignkey(ct_field="content_type", fk_field="object_pk") 

here snippet might out:

http://djangosnippets.org/snippets/1101/

from django.contrib.contenttypes.models import contenttype django.contrib.comments.models import comment django.db import connection  qn = connection.ops.quote_name  def qf(table, field): # quote table , field     return '%s.%s' % ( qn(table), qn(field) )  def comments_extra_count(queryset):      commented_model = queryset.model     contenttype = contenttype.objects.get_for_model(commented_model)     commented_table = commented_model._meta.db_table     comment_table = comment._meta.db_table      sql = '''select count(*) %s         %s=%%s , %s=%s     ''' % (         qn(comment_table),         qf(comment_table, 'content_type_id'),         qf(comment_table, 'object_pk'),         qf(commented_table, 'id')     )      return queryset.extra(         select={'comment_count': sql },         select_params=(contenttype.pk,)     ) 

you can implement inline, out defining these methods. end result annotated queryset attribute comment_count. sort:

qs_sorted_by_comment_count = comments_extra_count(some_qs).order_by('comment_count') 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -