为分层模型创建高效的数据库查询(django)

Izz*_*sin 3 django

考虑这个(django)模型:

class Source(models.Model):
   # Some other fields
   type = models.ForeignKey('Type')

class Type(models.Model):
    # Some other fields
    parent = models.ForeignKey('self')
Run Code Online (Sandbox Code Playgroud)

此模型具有自身的外键,从而创建层次结构.

假设我们有以下层次结构:

Website
    Blog
    News    
    Social Network
    Q&A
    Forum
Radio
    Government radio
    Pirate radio
    Commercial radio
    Internet radio
Run Code Online (Sandbox Code Playgroud)

如何有效地查询,这样如果我选择SourceType,我也找回Sources其中有一个Type是给定类型的孩子?

我试过遍历整棵树,但这显然不是很有效.

另一种选择是使用ManyToManyField并通过覆盖save()方法自动附加父类型.例如,如果选择"博客",则还会创建"网站"的记录.但这似乎对我来说太过分了.

ste*_*lis 5

django-mptt或django-treebeard是分层数据的好帮手.它们都会为模型添加额外的元数据,以实现高效查询.

如果你选择使用django-treebeard你的模型看起来像这样:

from django.db import models
from treebeard.mp_tree import MP_Node

class Source(models.Model):
    # Some other fields
    type = models.ForeignKey('Type')

class Type(MP_Node):
    # Some other fields
    name = models.CharField(max_length=100)

    # parent gets added automatically by treebeard
    # parent = models.ForeignKey('self', blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

并且可以像这样查询:

# get all Sources of Type type and descendants of type
type = Type.objects.get(name='Radio')
Source.objects.filter(type__in=type.get_descendants())
Run Code Online (Sandbox Code Playgroud)

有关更多可能的查询,请参阅https://tabo.pe/projects/django-treebeard/docs/tip/api.html