I have a django model called destination. I have another model called tours. Tours consist of destinations and other elements. In my VIEW for destinations, I want to have available a Tour List containing all the Tours associated with a particular destination. I’m not sure how to write it out. this is what I have:
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))
* Is where I’m trying to figure out how to query the Tour object. Tours and destinations have a many to many relationship.
get_object_or_404()will get you only a single object. Filter instead.