我想通過相關字段pk篩選資源。Tastypie - 通過相關pk篩選
例如,我有國家PK,我想從該國獲得所有城市。我怎麼能這樣做?
class CityResource(ModelResource):
class Meta:
queryset = City.objects.all()
resource_name = 'city'
fields = ['name','id']
filtering = {
#Something here
}
class CountryResource(ModelResource):
class Meta:
queryset = Country.objects.all()
resource_name = 'country'
fields = ['name','code2','id']
而且一個例子網址:
http://startuprepublik.pre.is/api/v1/city/?format=json&country__pk=4
任何想法?
你在你的城市模型上有國家ForeignKey嗎? – andrefsp
是的。我正在使用cities_light。 https://github.com/yourlabs/django-cities-light/blob/master/cities_light/models.py –