title: How to get a response of multiple objects using rest_framework and Django
body: <p>I'm new to the Django framework and the Django REST framework, but I got the basic setup and implementation running. It's working like a charm when I call the domain for single objects, e.g. <a href="http://mydomain.com/location/1" rel="noreferrer">http://mydomain.com/location/1</a> (where 1 is the primary key). This gives me the JSON response like: </p>
  
  <pre><code>{"id": 1, "location": "Berlin", "country": 2}
  </code></pre>
  
  <p>.. and <a href="http://mydomain.com/country/2" rel="noreferrer">http://mydomain.com/country/2</a> responses like: </p>
  
  <pre><code>{"id": 2, "country": "Germany"}
  </code></pre>
  
  <p><strong>What I need:</strong>
  Now I need to get multiple locations, e.g. when calling the domain <a href="http://mydomain.com/all_locations/" rel="noreferrer">http://mydomain.com/all_locations/</a>. I would expect a response like:</p>
  
  <pre><code>[
    {"id": 1, "location": "Berlin", "country": 2},
    {"id": 2, "location": "New York", "country": 4},
    {"id": 3, "location": "Barcelona", "country": 5},
    {"id": 4, "location": "Moscow", "country": 7}
  ]
  </code></pre>
  
  <p><strong>This is optional:</strong> In a second step I would love to have multiple countries and locations in one response when I call <a href="http://mydomain.com/mix_all_locations_countries/" rel="noreferrer">http://mydomain.com/mix_all_locations_countries/</a>, for example:</p>
  
  <pre><code>[
    {"locations":
      {"id": 1, "location": "Berlin", "country": 2},
      {"id": 2, "location": "New York", "country": 4},
      {"id": 3, "location": "Barcelona", "country": 5},
      {"id": 4, "location": "Moscow", "country": 7}
    },
    {"countries":
      {"id": 1, "country": "Brazil"}
      {"id": 2, "country": "Germany"},
      {"id": 3, "country": "Portugual"}
      {"id": 4, "country": "USA"},
      {"id": 5, "country": "Spain"},
      {"id": 6, "country": "Italy"}
      {"id": 7, "country": "Russia"}
    }
  ]
  </code></pre>
  
  <p>Here's my implementation so far (just showing the implementation of location):</p>
  
  <p>in <strong>models.py</strong>:</p>
  
  <pre><code>class Location(models.Model):
      # variable id and pk are always available
      location = models.CharField(max_length=100)
      country = models.ForeignKey("Country")
  </code></pre>
  
  <p>in <strong>serializers.py</strong>:</p>
  
  <pre><code>class LocationsSerializer(serializers.ModelSerializer):
      country_id = serializers.Field(source='country.id')
  
      class Meta:
          model = Location
          fields = (
              'id',
              'location',
              'country_id',
          )
  </code></pre>
  
  <p>in <strong>views.py</strong>:</p>
  
  <pre><code>class LocationAPIView(generics.RetrieveAPIView):
      queryset = Location.objects.all()
      serializer_class = LocationSerializer
  </code></pre>
  
  <p>in <strong>urls.py</strong>:</p>
  
  <pre><code>url(r'^location/(?P&lt;pk&gt;[0-9]+)/$', views.LocationAPIView.as_view(), name='LocationAPIView')
  </code></pre>
  
  <p><strong>What I tried:</strong> I think I do not need to modify the model and serializer, because it's working for single objects when calling the domain mentioned above. So I tried to implement a <code>LocationsViewSet</code> in <code>views.py</code> and added a new url in <code>urls.py</code>, but I failed. Any idea how I could implement it? Maybe just define a method in LocationAPIView and change define a url similar to this: </p>
  
  <pre><code>url(r'^all_locations/$', views.LocationAPIView.get_all_locations(), name='LocationAPIView')
  </code></pre>
  
  <p>Thanks in advance, I'll appreciate any help.</p>
  
  <p>Best regards,
  Michael</p>