zerohedge
zerohedge

Reputation: 3765

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

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?

Upvotes: 0

Views: 60

Answers (1)

zerohedge
zerohedge

Reputation: 3765

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.

Upvotes: 0

Related Questions