Django RSS feed: any way to cache M2M relationship to display in item_title?

54 views Asked by At

Example models Author and Book are linked via M2M. I've found a way to cache the relationship in items, but that doesn't really help because I need to display some info about Author in the Book feed:

def item_title(self, item):
    return f"{item.author_set.first().name} released {item.title}"

Any way to somehow cache the M2M relationship here?

1

There are 1 answers

0
zerohedge On BEST ANSWER

Could it be as easy as this?

def items(self, obj):
    …
    self.some_custom_dict = {x.id: x for x in releases}

def item_title(self, item):
    cached_with_relationship = self.some_custom_dict.get(item.id)

It seems to work after preliminary testing. Waiting for more informed opinions.