2014-03-30 88 views
0

我想如下篩選中使用函數Django視圖結果:Django的 - 無法解析關鍵字

views.py

def index(request): 
    european_team_list = Team.objects.all().filter(type = 'Europe') 
    context = {'european_team_list': european_team_list} 
    return render(request, 'myapp/index.html', context) 

admin.py

class Team(models.Model): 
    continent = models.CharField() 
    def _team_type(self): 
     if self.country = "Europe": 
      return "Europe" 
     else: 
      return "Not Europe" 
    team_type = property(_team_type) 
    ...other fields... 

然而,當我加載頁面時,出現錯誤「無法將關鍵字'team_type'解析爲字段,選項爲:」,然後列出除team_type之外的Team類中的所有字段。任何指導將不勝感激。

+0

另外'如果self.country = 「歐洲」'應該是'如果self.country = =「歐洲」'。 –

+0

你沒有從這裏顯示的任何代碼中得到這個錯誤。請顯示確切的代碼,以及完整的錯誤和追溯。 –

回答

1

簡單的答案是,你不能用filter()方法做到這一點。 filter()用於構建SQL查詢,並且只能對數據庫級別的對象進行操作。

所以你應該弄清楚如何使用數據庫值對你的查詢進行短語處理。目前還不清楚你的實際代碼是什麼,但它可能看起來像:

european_team_list = Team.objects.filter(continent='Europe') 

或:

european_team_list = Team.objects.filter(country__in=('France', 'Poland')) 
相關問題