Django:通过查询相关模型创建嵌套字典的最有效方法?
Django: Most efficient way to create a nested dictionary from querying related models?
在 Django 中,通过查询相关和 child 模型创建嵌套数据字典的最有效方法是什么?
比如我有以下型号:
- Parent
- Children
- 宠物
我看过 django 的 model_to_dict 方法,这很酷,所以我想我可以遍历每个级别的查询集并在每个级别上为每个实例创建一堆数据库调用,但是有没有有更好的方法吗?
例如,是否可以使用 "prefetch_related" 获取所有三个层级,因为它用于获取两个层级 ?
如果字典看起来像这样就好了:
[
{
"name": "Peter Parent",
"children": [
{
"name": "Chaden Child",
"pets": [
{
"name": "Fanny",
"type:": "fish"
},
{
"name": "Buster",
"type:": "bunny"
}
]
},
{
"name": "Charlete Child",
"pets": [
{
"name": "Dandy",
"type:": "dog"
}
]
}
]
}
]
编辑:
根据要求,这是模型的外观:
class Pet(models.Model):
name = models.CharField(max_length=50)
type = models.CharField(max_length=50)
def __str__(self):
return self.name
class Child(models.Model):
name = models.CharField(max_length=50)
pets = models.ManyToManyField(Pet)
def __str__(self):
return self.name
class Parent(models.Model):
name = models.CharField(max_length=50)
children = models.ManyToManyField(Child)
def __str__(self):
return self.name
这就是原始 sql 的样子:
SELECT pa.name, ch.name, pe.name, pe.type
FROM aarc_parent pa
JOIN aarc_parent_children pc ON pc.parent_id = pa.id
JOIN aarc_child ch ON ch.id = pc.child_id
JOIN aarc_child_pets cp ON cp.child_id = ch.id
JOIN aarc_pet pe ON pe.id = cp.pet_id
您可以将 prefetch_related
与列表解析一起使用。 prefetch_related
将有助于避免每次访问相关对象时进行额外查询。
parents = Parent.objects.all().prefetch_related('children__pets')
[{'name': parent.name, 'children': [{'name': child.name, 'pets': [{'name':pet.name, 'type':pet.type} for pet in child.pets.all()]} for child in parent.children.all()]} for parent in parents]
在 Django 中,通过查询相关和 child 模型创建嵌套数据字典的最有效方法是什么?
比如我有以下型号:
- Parent
- Children
- 宠物
- Children
我看过 django 的 model_to_dict 方法,这很酷,所以我想我可以遍历每个级别的查询集并在每个级别上为每个实例创建一堆数据库调用,但是有没有有更好的方法吗?
例如,是否可以使用 "prefetch_related" 获取所有三个层级,因为它用于获取两个层级
如果字典看起来像这样就好了:
[
{
"name": "Peter Parent",
"children": [
{
"name": "Chaden Child",
"pets": [
{
"name": "Fanny",
"type:": "fish"
},
{
"name": "Buster",
"type:": "bunny"
}
]
},
{
"name": "Charlete Child",
"pets": [
{
"name": "Dandy",
"type:": "dog"
}
]
}
]
}
]
编辑:
根据要求,这是模型的外观:
class Pet(models.Model):
name = models.CharField(max_length=50)
type = models.CharField(max_length=50)
def __str__(self):
return self.name
class Child(models.Model):
name = models.CharField(max_length=50)
pets = models.ManyToManyField(Pet)
def __str__(self):
return self.name
class Parent(models.Model):
name = models.CharField(max_length=50)
children = models.ManyToManyField(Child)
def __str__(self):
return self.name
这就是原始 sql 的样子:
SELECT pa.name, ch.name, pe.name, pe.type
FROM aarc_parent pa
JOIN aarc_parent_children pc ON pc.parent_id = pa.id
JOIN aarc_child ch ON ch.id = pc.child_id
JOIN aarc_child_pets cp ON cp.child_id = ch.id
JOIN aarc_pet pe ON pe.id = cp.pet_id
您可以将 prefetch_related
与列表解析一起使用。 prefetch_related
将有助于避免每次访问相关对象时进行额外查询。
parents = Parent.objects.all().prefetch_related('children__pets')
[{'name': parent.name, 'children': [{'name': child.name, 'pets': [{'name':pet.name, 'type':pet.type} for pet in child.pets.all()]} for child in parent.children.all()]} for parent in parents]