[Fixed]-Django: Returning objects when using a generic relationship as a through model

1👍

Try this one:

class Resource(models.Model):
    #...
    tag_items = GenericRelation(TaggedItem, related_query_name='resources')

    @property
    def platform(self):
        return Tag.objects.filter(
            taggetitem_items__resources=self,
            taggetitem_items__relationship=TaggedItem.PLATFORM,
        )

    @property
    def characteristics(self):
        return Tag.objects.filter(
            taggetitem_items__resources=self,
            taggetitem_items__relationship=TaggedItem.CHARACTERISTICS,
        )
👤Todor

0👍

@property
def characteristics(self):
    return Tag.objects.filter(pk__in=self.tag_items.filter(relationship=TaggedItem.CHARACTERISTICS).values('tag'))

Warning : Untested code, may not work but you get the idea.

Leave a comment