我想通过相关字段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
有什么想法吗?
答案 0 :(得分:3)
from tastypie.fields import ForeignKey
from tastypie.resources import ALL_WITH_RELATIONS
class CityResource(ModelResource):
country = ForeignKey("path.to.api.CountryResource", "country")
class Meta:
queryset = City.objects.all()
resource_name = 'city'
fields = ['name','id']
filtering = {
"country": ALL_WITH_RELATIONS
}
class CountryResource(ModelResource):
class Meta:
queryset = Country.objects.all()
resource_name = 'country'
fields = ['name','code2','id']
filtering = { "id": ALL }