如何过滤 Django 查询中的外键对象?
How to filter foreign key objects in django queries?
我有两个模型,一个通过 foreign key
与另一个相关联
class CapturedPrescriptionModel(ColModel):
p_id = models.IntegerField()
p_age = models.IntegerField()
p_gender = models.CharField(max_length=10)
p_care_type = models.CharField(max_length=100)
bacteria_id = models.ForeignKey(BacteriaListModel,
on_delete=models.CASCADE, null=True)
class SuggestedAntibioticsModel(ColModel):
prescription_id = models.ForeignKey(CapturedPrescriptionModel,
related_name='antibiotics',
on_delete=models.CASCADE)
cat_ids = models.TextField()
flag = models.IntegerField(default=0)
现在我想要所有 prescriptions
和 suggested antibiotics
其中 flag=1
我已经尝试使用 CapturedPrescriptionModel.objects.filter(antibiotics__flag=1)
但是过滤的是处方而不是查询集中的抗生素列表。
[
{
"id": 7,
"p_id": 0,
"p_age": 19,
"p_gender": "Male",
"p_care_type": "ICU",
"bacteria_id": null,
"antibiotics": [
{
"id": 188,
"cat_ids": "[]",
"flag": 0,
"antibiotic_id_id": 87,
"prescription_id_id": 7
},
{
"id": 187,
"cat_ids": "[]",
"flag": 1,
"antibiotic_id_id": 112,
"prescription_id_id": 7
},
......
]
}
....
]
我的预期结果是这样的
[
{
"id": 7,
"p_id": 0,
"p_age": 19,
"p_gender": "Male",
"p_care_type": "ICU",
"bacteria_id": null,
"antibiotics": [
{
"id": 187,
"cat_ids": "[]",
"flag": 1,
"antibiotic_id_id": 112,
"prescription_id_id": 7
}
]
}
....
]
收集所有药方:
prescriptions = CapturedPrescriptionModel.objects.all()
for prescription in prescriptions:
prescription.antibiotics = prescription.antibiotics.filter(flag=1)
# at this time presciptions should be prepared, just make sure to not save them...
您还可以扩展您的模型,使该列表具有 属性。
class CapturedPrescriptionModel(ColModel):
p_id = models.IntegerField()
p_age = models.IntegerField()
p_gender = models.CharField(max_length=10)
p_care_type = models.CharField(max_length=100)
bacteria_id = models.ForeignKey(BacteriaListModel,
on_delete=models.CASCADE, null=True)
@property
def flagged_antibiotics(self):
try:
return self.antibiotics.filter(flag=1)
except Exception:
return []
class SuggestedAntibioticsModel(ColModel):
prescription_id = models.ForeignKey(CapturedPrescriptionModel,
related_name='antibiotics',
on_delete=models.CASCADE)
cat_ids = models.TextField()
flag = models.IntegerField(default=0)
像这样的事情将是我的第一个想法
如果只想过滤相关对象而不是主要对象,则需要过滤 Prefetch
:
from django.db.models import Prefetch
CapturedPrescriptionModel.objects.prefetch_related(Prefetch(
'antibiotics',
queryset=SuggestedAntibioticsModel.objects.filter(flag=1)
)
然后您必须确保仅使用 prescription.antibiotics.all()
访问各个处方对象上的 antibiotics
,否则不会使用预取,您将再次获得所有抗生素。
我有两个模型,一个通过 foreign key
与另一个相关联
class CapturedPrescriptionModel(ColModel):
p_id = models.IntegerField()
p_age = models.IntegerField()
p_gender = models.CharField(max_length=10)
p_care_type = models.CharField(max_length=100)
bacteria_id = models.ForeignKey(BacteriaListModel,
on_delete=models.CASCADE, null=True)
class SuggestedAntibioticsModel(ColModel):
prescription_id = models.ForeignKey(CapturedPrescriptionModel,
related_name='antibiotics',
on_delete=models.CASCADE)
cat_ids = models.TextField()
flag = models.IntegerField(default=0)
现在我想要所有 prescriptions
和 suggested antibiotics
其中 flag=1
我已经尝试使用 CapturedPrescriptionModel.objects.filter(antibiotics__flag=1)
但是过滤的是处方而不是查询集中的抗生素列表。
[
{
"id": 7,
"p_id": 0,
"p_age": 19,
"p_gender": "Male",
"p_care_type": "ICU",
"bacteria_id": null,
"antibiotics": [
{
"id": 188,
"cat_ids": "[]",
"flag": 0,
"antibiotic_id_id": 87,
"prescription_id_id": 7
},
{
"id": 187,
"cat_ids": "[]",
"flag": 1,
"antibiotic_id_id": 112,
"prescription_id_id": 7
},
......
]
}
....
]
我的预期结果是这样的
[
{
"id": 7,
"p_id": 0,
"p_age": 19,
"p_gender": "Male",
"p_care_type": "ICU",
"bacteria_id": null,
"antibiotics": [
{
"id": 187,
"cat_ids": "[]",
"flag": 1,
"antibiotic_id_id": 112,
"prescription_id_id": 7
}
]
}
....
]
收集所有药方:
prescriptions = CapturedPrescriptionModel.objects.all()
for prescription in prescriptions:
prescription.antibiotics = prescription.antibiotics.filter(flag=1)
# at this time presciptions should be prepared, just make sure to not save them...
您还可以扩展您的模型,使该列表具有 属性。
class CapturedPrescriptionModel(ColModel):
p_id = models.IntegerField()
p_age = models.IntegerField()
p_gender = models.CharField(max_length=10)
p_care_type = models.CharField(max_length=100)
bacteria_id = models.ForeignKey(BacteriaListModel,
on_delete=models.CASCADE, null=True)
@property
def flagged_antibiotics(self):
try:
return self.antibiotics.filter(flag=1)
except Exception:
return []
class SuggestedAntibioticsModel(ColModel):
prescription_id = models.ForeignKey(CapturedPrescriptionModel,
related_name='antibiotics',
on_delete=models.CASCADE)
cat_ids = models.TextField()
flag = models.IntegerField(default=0)
像这样的事情将是我的第一个想法
如果只想过滤相关对象而不是主要对象,则需要过滤 Prefetch
:
from django.db.models import Prefetch
CapturedPrescriptionModel.objects.prefetch_related(Prefetch(
'antibiotics',
queryset=SuggestedAntibioticsModel.objects.filter(flag=1)
)
然后您必须确保仅使用 prescription.antibiotics.all()
访问各个处方对象上的 antibiotics
,否则不会使用预取,您将再次获得所有抗生素。