0

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?

zerohedge
  • 3,185
  • 4
  • 28
  • 63

1 Answers1

0

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.

zerohedge
  • 3,185
  • 4
  • 28
  • 63