从给定对象的所有站点获取 Django 评论

发布于 2024-12-22 01:46:55 字数 751 浏览 1 评论 0原文

我使用 Django-sites 框架在不同站点之间共享对象。这工作得很好,因为我能够在模型中定义多对多关系。

但是,在使用模板标签“render_comment_list”检索对象的评论(Django 评论)时,我只获取在该特定站点中发布的那些评论。这是预期的,但我还想获得针对在多个站点之间共享的该对象发布的其他评论。

深入研究 Django 注释的代码,似乎这是导致“问题”的方法:

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,
    )

我的问题是:

  • 改变行为的最简单方法是什么,以便模板标记“render_comment_list”显示对象的所有注释但不仅仅是针对特定网站的?
  • 我是否需要创建另一个模板标签并复制并复制它?粘贴 99% 的 Django-comments 模板标签代码?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愚人国度 2024-12-29 01:46:55

您不必复制并过去 99% 的模板标记代码,只需子类化 RenderCommentListNode 并覆盖您发现问题的 get_queryset_method 即可。然后复制 render_comment_list 函数,但使用您的子类。

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)

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)
逆蝶 2024-12-29 01:46:55

谢谢阿拉斯代尔!我进行了更改并且它正在发挥作用。为了清楚起见,编写整个代码(现在可以运行了!):

class RenderCommentListNodeAllSites(RenderCommentListNode):
    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,
        )

        # The is_public and is_removed fields are implementation details of the
        # built-in comment model's spam filtering system, so they might not
        # be present on a custom comment model subclass. If they exist, we
        # should filter on them.
        field_names = [f.name for f in self.comment_model._meta.fields]
        if 'is_public' in field_names:
            qs = qs.filter(is_public=True)
        if getattr(settings, 'COMMENTS_HIDE_REMOVED', True) and 'is_removed' in field_names:
            qs = qs.filter(is_removed=False)

        return qs

def render_comment_list_all_sites(parser, token):
    return RenderCommentListNodeAllSites.handle_token(parser, token)
register.tag(render_comment_list_all_sites)

Thanks Alasdair! I made the changes and it is working. Writing the whole code (now it works!) for clarity:

class RenderCommentListNodeAllSites(RenderCommentListNode):
    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,
        )

        # The is_public and is_removed fields are implementation details of the
        # built-in comment model's spam filtering system, so they might not
        # be present on a custom comment model subclass. If they exist, we
        # should filter on them.
        field_names = [f.name for f in self.comment_model._meta.fields]
        if 'is_public' in field_names:
            qs = qs.filter(is_public=True)
        if getattr(settings, 'COMMENTS_HIDE_REMOVED', True) and 'is_removed' in field_names:
            qs = qs.filter(is_removed=False)

        return qs

def render_comment_list_all_sites(parser, token):
    return RenderCommentListNodeAllSites.handle_token(parser, token)
register.tag(render_comment_list_all_sites)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文