Reputation: 10249
Using django-tagging, for an object that has multiple tags assigned to it, how can I return a simple list of tag names?
object.tags() returns an object that is not easily translated to json, and TaggableManager is not iterable.
Any other ways?
Upvotes: 4
Views: 1764
Reputation: 1494
tags_list = []
for tag in foobar.tags.all():
tags_list.append(tag.name)
Upvotes: 0
Reputation: 10249
There is a undocumented function in TaggableManager called 'get_query_set', from which it is easy to get the list:
tagsList = []
for tag in foobar.tags.get_query_set():
tagsList.append(tag.name)
Upvotes: 6
Reputation: 1887
First variant
class MyClass(models.Model)
...
def get_tag_names(self):
return [tag.name for tag in Tag.objects.get_for_object(self)]
Second variant:
class MyClass(models.Model)
...
def get_tag_names(self):
return Tag.objects.get_for_object(self).values_list('name', flat=True)
I think both should work.
Upvotes: 3