2013-10-30 93 views
0

我有一個小Django應用程序,當我用django orm查詢我的數據庫時,我得到我的對象列表。 我想要的是查詢匹配的db列的名稱。 可能嗎?從orm查詢檢索到的對象獲取字段名稱

在此先感謝!

results = Verbs.objects.filter(
     Q(fps__icontains = " " + word_name + " ") | Q(fps__endswith = " " + word_name) | Q(fps__startswith = word_name + " ") | 
     Q(sps__icontains = " " + word_name + " ") | Q(sps__endswith = " " + word_name) | Q(sps__startswith = word_name + " ") | 
     Q(tps__icontains = " " + word_name + " ") | Q(tps__endswith = " " + word_name) | Q(tps__startswith = word_name + " ") | 
     Q(fpp__icontains = " " + word_name + " ") | Q(fpp__endswith = " " + word_name) | Q(fpp__startswith = word_name + " ") | 
     Q(spp__icontains = " " + word_name + " ") | Q(spp__endswith = " " + word_name) | Q(spp__startswith = word_name + " ") | 
     Q(tpp__icontains = " " + word_name + " ") | Q(tpp__endswith = " " + word_name) | Q(tpp__startswith = word_name + " ") 
     ).all() 

我想在這裏爲匹配查詢字段的名稱。例如:fps或FPP ...

+1

您能否展示您的查詢以及您想從中獲取哪種結果? – nickzam

回答

2

我想這是你想要的 - 每個返回行,將打印匹配字段,縮進:

fields = ('fps', 'sps', 'tps', 'fpp', 'spp', 'tpp') 
for r in results.values(): 
    print 'Row with id=%s:' % r['id'] 
    for f in fields: 
     if word_name in str(r[f]): 
      print ' Field %s matches' % f 

編輯,以考慮非字符串值並只看到所需的領域。

+0

This return me: 類型'long'的參數不可迭代 –