Fetching Django-comments from all sites for a given object
-
20-03-2021 - |
문제
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
해결책
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)
다른 팁
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)