3 回答
TA贡献1772条经验 获得超5个赞
你的表是假设名称A和B分别则:
try:
obj = A.objects.get(name='John')
if B.objects.filter(name='John').exists():
print obj.code # found a match and now print code.
except:
pass
TA贡献1811条经验 获得超5个赞
让我们假设TableA和TableB是 Django 模型。然后,您的查询可能如下所示:
a_name = 'John'
it_matches_on_b = ( Table_B
.objects
.filter( name = a_name )
.exists()
)
fist_a = ( Table_A
.objects
.filter( name = a_name )
.first()
)
your_code = fist_a.code if it_matches_on_b and fist_a != None else None
我不注释代码,因为它是不言自明的。但是,如果您有评论,请在评论中写下问题。
添加回答
举报
