我有3张桌子:-
class Profile(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField()
.....
#some more field
.....
class ProfileTestMapping(models.Model):
id = models.AutoField(primary_key=True)
profile = models.ForeignKey(Profile)
test = models.ForeignKey(Test)
.....
#some more field
....
class Test(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField()
.....
#some more field
....
1个配置文件有很多测试。所以我想要这样的数据:-
This is a raw array I am writing for the example purpose.
profileList = [
{
profileName: 'any-name',
......
otherProfileDetails
.......
testList: [
{
name: testName,
id: 463743
},
{
name: testName2,
id: 463743
}
]
}
]
我正在使用count
将annotate
的测试与配置文件映射,但是没有获取与配置文件映射的测试的详细信息。
已更新:- 当前查询我得到的是计数的数目,而不是映射的测试数据的列表:-
Profile.objects.filter(isDisable=0).value('id', 'name').annotate(
testCount = Count('profiletestmapping__test_id')
testList = //Unable to get list of related data
)
答案 0 :(得分:0)
您可以在选择个人资料时使用prefetch_related参加所有测试。
Profile.objects.prefetch_related(
'ProfileTestMapping_set__test'
).all()