首页 > 解决方案 > Django RSS 提要:有什么方法可以缓存 M2M 关系以显示在 item_title 中?

问题描述

示例模型AuthorBook通过 M2M 链接。我找到了一种缓存关系的方法,但这并没有真正帮助,因为我需要在提要items中显示一些信息:AuthorBook

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

有什么办法可以在这里缓存 M2M 关系吗?

标签: djangodjango-rssdjango-syndication

解决方案


能这么简单吗?

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)

经过初步测试,它似乎可以工作。等待更详细的意见。


推荐阅读