문제

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!

도움이 되었습니까?

해결책

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"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top