3 回答

TA貢獻(xiàn)1772條經(jīng)驗 獲得超5個贊
你的表是假設(shè)名稱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貢獻(xiàn)1811條經(jīng)驗 獲得超5個贊
讓我們假設(shè)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
我不注釋代碼,因為它是不言自明的。但是,如果您有評論,請在評論中寫下問題。
添加回答
舉報