2011-03-22 44 views
0

我有一個稱爲目標的django模型。我有另一種叫做旅遊的模式。旅遊包括目的地和其他元素。在我的目的地查看中,我想要提供包含與特定目的地相關聯的所有Tours的Tour List。我不知道如何寫出來。這就是我:查看DJANGO視圖中的對象

from django.template import Context, loader 
from ceibelize.destinations.models import Destination 
from ceibelize.tours.models import Tour 
from django.http import HttpResponse 
from django.shortcuts import render_to_response, get_object_or_404 

def detail(request, destination_id): 
    dest_object = get_object_or_404(Destination, pk=destination_id) ***** 
    tours_in = get_object_or_404(Tour, Tour.destination=destination_id) 
    t = loader.get_template('destinations/detail.html') 
    c = Context({ 
     'dest_object':dest_object, 
    }) 
    return HttpResponse(t.render(c)) 

*就是我試圖找出如何查詢旅遊對象。旅遊和目的地有多對多的關係。

回答

0

get_object_or_404()會讓你只有一個對象。改爲過濾。

tours_in = Tour.objects.filter(destination=destination_id) 
+0

謝謝! :) 聰明小子! :d – Fel 2011-03-22 20:55:44