Python 检查值是否存在于另一个表中
对不起,我有点怀疑,我是学生。我有这种情况Python 检查值是否存在于另一个表中,python,django,Python,Django,对不起,我有点怀疑,我是学生。我有这种情况 class Equipment(): id name ... class Alert(): id ... pin value equipment_id 表设备-警报没有关系: 我需要检查警报表中是否存在设备表的id 如果需要返回A,如果没有返回B,则返回th到模板 很难说你想做什么,但是你可以做一系列的工
class Equipment():
id
name
...
class Alert():
id
...
pin
value
equipment_id
表设备-警报没有关系:
我需要检查警报表中是否存在设备表的id
如果需要返回A,如果没有返回B,则返回th到模板
很难说你想做什么,但是你可以做一系列的工作
# will give all equipment_ids in all alerts
alerted_equipment_ids = Alert.objects.values_list('equipment_id', flat=True).distinct()
# will filter Equipment and give those with alerts
alerted_equipment = Equipment.objects.filter(pk__in=alerted_equipment_ids)
# will get alerts that has a valid equipment-ID
alerts_for_alerted_equipment = Alert.objects.filter(equipment_id__in=alerted_equipment.values_list('pk', flat=True))
你试了什么?