情况与Django prefetch_related children of children相同,但问题不同:
我有一个模型Node
,看起来像这样:
class Node(models.Model):
parent = models.ForeignKey('self', related_name='children', on_delete=models.CASCADE, null=True)
一个节点可以有几个孩子,每个孩子可以有自己的孩子。
我想做这样的事情:
def cache_children(node):
for child in node.children.all():
cache_children(child)
root_node = Node.objects.prefetch_related('children').get(pk=my_node_id)
all_nodes = Node.objects.all() # get all the nodes in a single query
# Currently: hit database for every loop
# Would like: to somehow use the already loaded data from all_nodes
cache_children(root_node)
由于我已经获取了all_nodes
查询中的所有节点,因此我想重用该查询中的缓存数据,而不是每次都执行一个新的查询。
有什么办法可以实现?
答案 0 :(得分:1)
树状结构中的数据并不是很适合关系数据库,但是有一些策略可以解决此问题-请参阅tree implemenations in the docs of django-treebeard上的章节。
如果树不是太大,则可以将树完全存储在python dict中并缓存结果。
示例(未经测试-使数据结构适合您的喜好...):
from django.core.cache import cache
# ...
def get_children(nodes, node):
node['children'] = [n for n in nodes if n['parent']==node['id']]
for child_node in node['children']:
child_node = get_children(nodes, child_node)
return node
def get_tree(timeout_in_seconds=3600)
tree = cache.get('your_cache_key')
if not tree:
# this creates a list of dicts with the instances values - one DB hit!
all_nodes = list(Node.objects.all().values())
root_node = [n for n in nodes if n['parent']==None][0]
tree = get_children(all_nodes, root_node)
cache.set('your_cache_key', tree, timeout_in_seconds)
return tree
答案 1 :(得分:0)
我设法使其工作,并用2个db调用填充了整个树:
def populate_prefetch_cache(node, all_nodes):
children = [child for child in all_nodes if child.parent_id==node.id]
# will not have the attribute if no prefetch has been done
if not hasattr(node, '_prefetched_objects_cache'):
node._prefetched_objects_cache = {}
# Key to using local data to populate a prefetch!
node._prefetched_objects_cache['children'] = children
node._prefetch_done = True
for child in node.children.all():
populate_prefetch_cache(child , all_nodes )
all_nodes = list(Node.objects.all()) # Hit database once
root_node = Node.objects.get(pk=my_node_id) # Hit database once
# Does not hit the database and properly populates the children field
populate_prefetch_cache(root_node, all_nodes)
由于以下答案,我发现了_prefetched_objects_cache
属性:Django: Adding objects to a related set without saving to DB