从给定对象的所有站点获取 Django 评论
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必复制并过去 99% 的模板标记代码,只需子类化 RenderCommentListNode 并覆盖您发现问题的
get_queryset_method
即可。然后复制 render_comment_list 函数,但使用您的子类。You don't have to copy and past 99% of the template tag code, just subclass
RenderCommentListNode
and override theget_queryset_method
where you identified the problem. Then copy therender_comment_list
function, but use your child class.谢谢阿拉斯代尔!我进行了更改并且它正在发挥作用。为了清楚起见,编写整个代码(现在可以运行了!):
Thanks Alasdair! I made the changes and it is working. Writing the whole code (now it works!) for clarity: