我使用Django-sites框架在不同站点之间共享对象。这很好,因为我能够在我的模型中定义多对多关系。
但是,在使用模板标记'render_comment_list'检索对象的注释(Django-comments)时,我只获得那些在该特定站点中发布的注释。这是预期的,但我还想获得为该对象发布的其他评论,这些评论在多个站点之间共享。
深入研究Django-comments的代码,似乎这是导致'问题'的方法:
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,
)
我的问题是:
由于
答案 0 :(得分:1)
您不必复制和浏览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)
答案 1 :(得分:0)
谢谢Alasdair!我做了改动,它正在发挥作用。为了清楚起见,编写整个代码(现在它可以工作!):</ p>
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)