型号:
class Category(models.Model):
name = models.CharField()
parent = models.ForeignKey('self', null=True, related_name='subcategories')
数据库在每个函数调用中都被命中:
def get_children(elem, lst):
lst.append(elem.pk)
children = elem.subcategories.all()
if not children: return
for c in children:
get_children(c, lst)
lst=[]
get_children(Category.objects.prefetch_related('subcategories').get(pk=1), lst) # prefetch related is not working
如何在一个查询中获取所有表?