django - Django RSS 提要:有什么方法可以缓存 M2M 关系以显示在 item_title 中?
问题描述
示例模型Author
并Book
通过 M2M 链接。我找到了一种缓存关系的方法,但这并没有真正帮助,因为我需要在提要items
中显示一些信息:Author
Book
def item_title(self, item):
return f"{item.author_set.first().name} released {item.title}"
有什么办法可以在这里缓存 M2M 关系吗?
解决方案
能这么简单吗?
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)
经过初步测试,它似乎可以工作。等待更详细的意见。
推荐阅读
- python - Python,用数据框列匹配和替换字符串列表
- android - 如何在片段中显示数据在节点子节点内的数据?
- reactjs - 检测 React JS 中字段的多次点击
- r - 在R中的一帧中合并两个逗号分隔的列
- swift - 与 UnsafeMutablePointer 一起使用时,Swift 中的局部变量是否隐式初始化为 nil?
- python - 如何在 Python 中获取视频文件路径
- sql-server - Azure SQL 数据库未找到或无法访问的问题
- git - 如何 git 合并,但不使用最新的提交
- php - 同一页面上的 AJAX 表单的动态数量
- angular - 在Angular 6中测试httpClient的继承