简体   繁体   English

Django查询Django 1.2的嵌套时间列表

[英]Django querying a nested list of times Django 1.2

I have a list of objects which form a tree structure through foreign key relationships. 我有一个通过外键关系形成树结构的对象列表。

parent = models.ForeignKey( 'Category', related_name = 'child_set', null = True )

So each category object has children which are also category objects. 因此,每个类别对象都有孩子,这些孩子也是类别对象。 I want to pull them out of the DB in such a way as to hit the database as few times as possible. 我想将它们从数据库中拉出,以便尽可能少地访问数据库。 My former code looked like this: 我以前的代码如下所示:

def get_nested_categories(self):
    categories = []
    for child in self.child_set.all():
         categories.append(child)
         categories += child.get_nested_categories()
    return categories

But this causes a many hits to the database and causes slowness, especially as my category list has grown beyond what I expected it to be. 但是,这对数据库造成了很多打击,并导致运行缓慢,尤其是当我的类别列表已经超出我的预期范围时。 Is there a way to optimize this code? 有没有一种方法可以优化此代码?

This is not a Django ORM problem, but rather a lower-level relational modeling problem. 这不是Django ORM问题,而是一个较低级的关系建模问题。 Check out this related question on nested sets and adjacency lists for a good overview of your options in a relational database. 请查看有关嵌套集和邻接表的相关问题,以更好地了解关系数据库中的选项。 Short of that you could pursue a document-oriented (NoSQL) strategy for this particular data structure. 除此之外,您可以针对此特定数据结构采用面向文档(NoSQL)的策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM