比较来自两个不同模型的两个相似字段
Compare two alike fields from two separate models
给定 django 1.8、python 2.7.5、postgresql 和以下通用模型:
class restAPI(models.Model):
rest_id = models.CharField(max_length=20)
rest_host = models.CharField(max_length=20)
rest_mode = models.CharField(max_length=20)
rest_state = models.CharField(max_length=20)
class soapAPI(models.Model):
soap_id = models.CharField(max_length=20)
soap_host = models.CharField(max_length=20)
soap_asset = models.CharField(max_length=20)
soap_state = models.CharField(max_length=20)
在完美世界中,soapAPI.soap_host
和 restAPI.rest_host
将完美匹配。然而,这种情况很少见。我正在尝试查找并 return soapAPI 中存在但 restAPI 中不存在的任何主机。我目前有一种工作方法,在将数据保存到它自己的 django 模型之前,我使用 python 解析这些数据,但我必须相信我可以使用模型本身来做到这一点(我认为这会更多效率也很高)。
如何使用提供的 orm return soapAPI
的 ENTIRE 模型,其中 restAPI.rest_host
中缺少 soapAPI.soap_host
通过 django 或使用 .raw()
,最好与 orm 一起使用?任何和所有帮助将不胜感激,因为这对我来说是一次学习经历。提前谢谢你。
EDIT1
完全愿意将关系模型作为某种形式的答案。
EDIT2
保持开放,因为我不得不相信有比多个查询集更好的方法来做到这一点。
EDIT3
另外还有办法做多个 __in
之类的吗?我实际上需要 return 整个 soapAPI
模型,其中 soapAPI.soap_host
在 restAPI.rest_host
AND soapAPI.soap_state
中缺失 'Live'.
应该是这样的:
rest_hosts = restAPI.objects.only("rest_host")
excluded_rest_hosts = [host.rest_host for host in rest_hosts]
missing_soap_hosts = soapAPI.objects.exclude(soap_host__in=excluded_rest_hosts)
希望对您有所帮助!
鉴于@HassenPy 给出的答案,这是我能想到的最好答案:
excludehosts = restAPI.objects.values_list('rest_host', flat=True)
missinghosts = soapAPI.objects.exclude(soap_host__in=excludehosts)
编辑
为了回答我自己的编辑 3,这是我能想到的最好的答案:
excludehosts = restAPI.objects.values_list('rest_host', flat=True)
missinghosts = soapAPI.objects.exclude(soap_host__in=excludehosts)
missinghosts.filter(soap_state='Live')
尽管以下方法也有效:
excludehosts = restAPI.objects.values_list('rest_host', flat=True)
soapAPI.objects.filter(soap_state='Live').exclude(soap_host__in=excludehosts)
不确定哪个在技术上更好,因为我不能诚实地确定 return 时间的差异,但是嘿工作解决方案。
给定 django 1.8、python 2.7.5、postgresql 和以下通用模型:
class restAPI(models.Model):
rest_id = models.CharField(max_length=20)
rest_host = models.CharField(max_length=20)
rest_mode = models.CharField(max_length=20)
rest_state = models.CharField(max_length=20)
class soapAPI(models.Model):
soap_id = models.CharField(max_length=20)
soap_host = models.CharField(max_length=20)
soap_asset = models.CharField(max_length=20)
soap_state = models.CharField(max_length=20)
在完美世界中,soapAPI.soap_host
和 restAPI.rest_host
将完美匹配。然而,这种情况很少见。我正在尝试查找并 return soapAPI 中存在但 restAPI 中不存在的任何主机。我目前有一种工作方法,在将数据保存到它自己的 django 模型之前,我使用 python 解析这些数据,但我必须相信我可以使用模型本身来做到这一点(我认为这会更多效率也很高)。
如何使用提供的 orm return soapAPI
的 ENTIRE 模型,其中 restAPI.rest_host
中缺少 soapAPI.soap_host
通过 django 或使用 .raw()
,最好与 orm 一起使用?任何和所有帮助将不胜感激,因为这对我来说是一次学习经历。提前谢谢你。
EDIT1
完全愿意将关系模型作为某种形式的答案。
EDIT2
保持开放,因为我不得不相信有比多个查询集更好的方法来做到这一点。
EDIT3
另外还有办法做多个 __in
之类的吗?我实际上需要 return 整个 soapAPI
模型,其中 soapAPI.soap_host
在 restAPI.rest_host
AND soapAPI.soap_state
中缺失 'Live'.
应该是这样的:
rest_hosts = restAPI.objects.only("rest_host") excluded_rest_hosts = [host.rest_host for host in rest_hosts] missing_soap_hosts = soapAPI.objects.exclude(soap_host__in=excluded_rest_hosts)
希望对您有所帮助!
鉴于@HassenPy 给出的答案,这是我能想到的最好答案:
excludehosts = restAPI.objects.values_list('rest_host', flat=True)
missinghosts = soapAPI.objects.exclude(soap_host__in=excludehosts)
编辑
为了回答我自己的编辑 3,这是我能想到的最好的答案:
excludehosts = restAPI.objects.values_list('rest_host', flat=True)
missinghosts = soapAPI.objects.exclude(soap_host__in=excludehosts)
missinghosts.filter(soap_state='Live')
尽管以下方法也有效:
excludehosts = restAPI.objects.values_list('rest_host', flat=True)
soapAPI.objects.filter(soap_state='Live').exclude(soap_host__in=excludehosts)
不确定哪个在技术上更好,因为我不能诚实地确定 return 时间的差异,但是嘿工作解决方案。