1
votes

I'm trying to get tags as a queryset to loop through and get another BlogPost that has the same tags, but I get

def get_related_content(self):
    related_content = []
    if self.tags:
        for tag in self.tags:
            related_content += BlogDetailPage.objects.live().filter(tags__name=tag)
            related_content += OfferDescription.objects.live().filter(tags__name=tag)
    else:
        return related_content

I get this error:

'_ClusterTaggableManager' object is not iterable

I have also tried to use the django-taggit get_query_set() method, but it doesn't work, seems not to be included in the wagtail.

def get_related_content(self):
    related_content = []
    if self.tags:
        for tag in self.tags.get_query_set():
            related_content += BlogDetailPage.objects.live().filter(tags__name=tag)
            related_content += OfferDescription.objects.live().filter(tags__name=tag)
    else:
        return related_content

'_ClusterTaggableManager' object has no attribute 'get_query_set'

How can I loop over all self.tags and get other page models that use similar tags?

My Models:

class OfferDescriptionPageTag(TaggedItemBase):
    content_object = ParentalKey('OfferDescription', on_delete=models.CASCADE, related_name='tagged_items')

class OfferDescription(Page):

    tags = ClusterTaggableManager(through=OfferDescriptionPageTag, blank=True)
1

1 Answers

3
votes

In your first example, try for tag in self.tags.all():.

Your line if self.tags won't help, because the tags relationship is a manager, not a queryset, and will always evaluate to True. You could test if self.tags.exists().

In your second code example, the method should be .get_queryset(). See https://github.com/wagtail/django-modelcluster/blob/master/modelcluster/contrib/taggit.py#L26.