Django - Populate model instance related field fro

2019-07-12 18:21发布

Same situation as Django prefetch_related children of children but different question:

I have a model Node that looks something like that:

class Node(models.Model):
    parent = models.ForeignKey('self', related_name='children', on_delete=models.CASCADE, null=True)

A Node can have several children, and each of these children can have its own children.

I would like to do something like that:

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)  

As I already grabbed all the nodes in the all_nodes query, I would like to reuse the cached data from this query instead of performing a new one each time.

Is there any way to achieve that?

2条回答
小情绪 Triste *
2楼-- · 2019-07-12 19:04

Data in a tree like structure is not really well suited for a relational database, there are however some strategies to solve this - see the chapter on tree implemenations in the docs of django-treebeard.

If your tree isn't too big, you could totally store the tree in a python dict and cache the results.

Example (untested - adapt the data structure to your liking...):

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
  • Of course you have to have your cache enabled
  • you could invalidate the cache in your Node.save method
查看更多
我只想做你的唯一
3楼-- · 2019-07-12 19:05

I managed to make it work this way and populate the whole tree with 2 db calls:

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)

I discovered the _prefetched_objects_cache attribute thanks to this answer: Django: Adding objects to a related set without saving to DB

查看更多
登录 后发表回答