Search code examples
djangodjango-commentsdjango-sites

Fetching Django-comments from all sites for a given object


I am sharing objects between different sites using the Django-sites framework. This works fine because I was able to define a many-to-many relationship within my models.

However, while retrieving the comments (Django-comments) for the objects using the template tag 'render_comment_list', I only get those comments which where posted in that particular site. This is expected, but I would like also to get those other comments that were posted for that object which is shared among multiple sites.

Digging into the code of Django-comments, it seems that this is the method causing the 'problem':

def get_query_set(self, context):
    ctype, object_pk = self.get_target_ctype_pk(context)
    if not object_pk:
        return self.comment_model.objects.none()

    qs = self.comment_model.objects.filter(
        content_type = ctype,
        object_pk    = smart_unicode(object_pk),
        site__pk     = settings.SITE_ID,
    )

My questions are:

  • What would be the easiest way to change the behavior so the template tag 'render_comment_list' displays all the comments for an object but not just the ones for a particular site?
  • Do I need to create another template tag and copy & paste 99% of the Django-comments template tag code?

Thanks


Solution

  • You don't have to copy and past 99% of the template tag code, just subclass RenderCommentListNode and override the get_queryset_method where you identified the problem. Then copy the render_comment_list function, but use your child class.

    class RenderCommentListNodeAllSites(RenderCommnetListNode):
        def get_query_set(self, context):
            ctype, object_pk = self.get_target_ctype_pk(context)
            if not object_pk:
                return self.comment_model.objects.none()
    
            qs = self.comment_model.objects.filter(
                content_type = ctype,
                object_pk    = smart_unicode(object_pk),
            )
    
    def render_comment_list_all_sites(parser, token):
        return RenderCommentListNodeAllSites.handle_token(parser, token)
    register.tag(render_comment_list_all_sites)