Search code examples
djangotaggingdjango-tagging

How to display related tags in django tagging?


i am using django tagging. can anyone give any example as to how can i show the related tags, when the object related to a particular tag is displayed? Something like similar tags in stackoverflow.

Thank you!


Solution

  • You can use the get_related manager which will:

    Retrieve a list of instances of the specified model which share tags with the model instance obj ordered by the number of shared tags in descending order.

    To use this you could create a template tag such as:

       @register.inclusion_tag(your_template)
       def related_objects(object, limit=3):
           objects = TaggedItem.objects.get_related(object,object.__class__)
           return {'objects': objects[:limit]}
    

    Edit for comment

    to get a list of similar tags you can use related_for_model, which will return "other tags used by items which have all the given tags"