title: Django and models with multiple foreign keys
body: <p>I am new to Django and I've been impressed so far by its capabilities. I am playing with more complex models and I am have problem to use them properly. Using Django 1.3, I am trying to write a summary page which would present the three models below with the following structure. In other words, a list of trips with their destinations and activities.</p>
  
  <ul>
  <li>Trip 1
  <ul>
  <li>Destination 1</li>
  <li>Destination 2</li>
  <li>Activity 1</li>
  </ul></li>
  <li>Trip 2
  <ul>
  <li>Destination 1</li>
  <li>Activity 2</li>
  </ul></li>
  </ul>
  
  <p><strong>Models</strong><br/></p>
  
  <ul>
  <li>Trip &lt;-> TripDestination &lt;-> Destination (a trip can have multiple destinations)</li>
  <li>Activity -> Trip, Activity -> Destination (an activity is defined for a trip at a specific location/destination)</li>
  </ul>
  
  <pre><code>    class Destination(models.Model):
          city_name=models.CharField()
  
      class Trip(models.Model):
          departing_on=models.DateField()
          returning_on=models.DateField()
          destinations=models.ManyToManyField(Destination)
  
      class Activity(models.Model):
          destination=models.ForeignKey(Destination, null=False)
          trip=models.ForeignKey(Trip, null=False)</code></pre>
  
  <p>I am trying to write a view which would generate a page with the structure presented above. The main problem I am having right now is to display the activities for a specific trip and destination. As you can see in the code below, I am building a dictionary and I doubt it is the right thing to do. In addition, the view becomes</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>def list_trip(request, template_name = 'trip-list.html'):
      trips = Trip.objects.all()
  
      # Build a dictionary for activities -- Is this the right thing to do?
      activities = Activity.objects.filter(trip__in=trips)
      activities_by_trips = dict()
      for activity in activities:
          if activity.trip_id not in activities_by_trips:
              activities_by_trips[activity.trip_id] = dict()
  
          if activity.destination_id not in activities_by_trips[activity.trip_id]:
              activities_by_trips[activity.trip_id][activity.destination_id] = []
  
          activities_by_trips[activity.trip_id][activity.destination_id].append(activity)
  
      return render_to_response(template_name, {
          'page_title': 'List of trips',
          'trips': trips,
          'activities_by_trips': activities_by_trips,
      })</code></pre>
  
  <p><strong>Template</strong></p>
  
  <pre><code>
  {% block content %}
      {% for trip in trips %}
          {{ trip.id }} - {{ trip.name }}
  
          {% for destination in trip.destinations.all %}
              {{ destination.city_name }}
  
              ** This is terrible code -- How to fix that **
              {% for key, value in activities_by_trips|dict_lookup:trip.id %}
                  {% if value %}
                      {% for key_prime, value_prime in value|dict_lookup:destination.id %}
                         {{ value_prime.description }}
                      {% endfor %}
                  {% endif %}
              {% endfor %}
          {% endfor %}
      {% endfor %}
  {% endblock %}
  </code></pre>
  
  <p>In brief, can someone please help me to get a summary of all the trips and activities? What's the best way to accomplish that? Is the model correct?</p>
  
  <p>Thanks!</p>