Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查值是否存在于另一个表中_Python_Django - Fatal编程技术网

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))

你试了什么?