Django: Filter to get linked object from comment

While I'm on the topic of linking objects to comments, a handy filter to have is the ability to convert an object into the linked object.

@register.filter
def get_comment_object(comment):
"""
This is the reverse, it gets an object from the comment
"""
return comment.content_type.get_object_for_this_type(pk = comment.object_pk)

To use it, just call:

{{ comment|get_comment_object }}

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog