我是Django框架和Django REST框架的新手,但我已經完成了基本的安裝和實現運行。當我爲單個對象調用域時,它的功能就像一個魅力一樣,例如http://mydomain.com/location/1(其中1是主鍵)。這給了我喜歡的JSON響應:如何使用rest_framework和Django獲得多個對象的響應
{"id": 1, "location": "Berlin", "country": 2}
..和http://mydomain.com/country/2樣應答:
{"id": 2, "country": "Germany"}
我需要什麼: 現在我需要得到多個位置,例如當調用域名http://mydomain.com/all_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}
]
這是可選的:在第二步我很想有多個國家和地區的一個迴應時,我打電話http://mydomain.com/mix_all_locations_countries/,例如:
[
{"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"}
}
]
這是我迄今爲止的實現(只顯示位置的實現):
in models.py:
class Location(models.Model):
# variable id and pk are always available
location = models.CharField(max_length=100)
country = models.ForeignKey("Country")
在serializers.py:
class LocationsSerializer(serializers.ModelSerializer):
country_id = serializers.Field(source='country.id')
class Meta:
model = Location
fields = (
'id',
'location',
'country_id',
)
在views.py:
class LocationAPIView(generics.RetrieveAPIView):
queryset = Location.objects.all()
serializer_class = LocationSerializer
在urls.py:
url(r'^location/(?P<pk>[0-9]+)/$', views.LocationAPIView.as_view(), name='LocationAPIView')
我試過了:我想我不需要修改模型和序列化程序,因爲它在調用上面提到的域時用於單個對象。所以我試圖在views.py
中實現LocationsViewSet
,並在urls.py
中添加了一個新的url,但是我失敗了。任何想法如何我可以實現它?也許只是定義LocationAPIView的方法和改變定義類似這樣的URL:
url(r'^all_locations/$', views.LocationAPIView.get_all_locations(), name='LocationAPIView')
在此先感謝,我會感激任何幫助。
最好的問候, 邁克爾
謝謝湯姆!我嘗試了你的方法,第二部分與CombinedAPIView工作很好!但是,第一個建議不起作用。這可能是因爲我正在使用generics.RetrieveAPIView?以下是錯誤: '預期視圖LocationListAPIView將使用名爲「pk」的URL關鍵字參數進行調用。修正你的URL conf,或正確設置視圖上的'.lookup_field'屬性。' – Michael
之後,我只是試圖使用CombinedAPIView來測試URL: 'url(r'^ location/$',views.CombinedAPIView.as_view (),name ='location-list'),' 這可行,但它不是解決方案:)希望你能幫我解決這個錯誤。提前致謝。 – Michael
好吧,我修正了這個使用APIView代替。無論如何,你有一個想法如何解決它與generics.RetrieveAPIView ??這裏是爲我工作的解決方案: '類LocationListAPIView(APIView): DEF得到(個體經營,請求): 位置= Location.objects.all() location_serializer = LocationSerializer(地點,許多= TRUE) return響應({'locations':location_serializer.data, })' – Michael