Python Tastypie-通过相关pk进行过滤
我想通过相关字段pk筛选资源 例如,我有国家pk,我想得到该国的所有城市。我怎么能这么做Python Tastypie-通过相关pk进行过滤,python,tastypie,Python,Tastypie,我想通过相关字段pk筛选资源 例如,我有国家pk,我想得到该国的所有城市。我怎么能这么做 class CityResource(ModelResource): class Meta: queryset = City.objects.all() resource_name = 'city' fields = ['name','id'] filtering = { #Something here
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']
以及一个示例URL:
http://startuprepublik.pre.is/api/v1/city/?format=json&country__pk=4
有什么想法吗?您的城市模型上有country ForeignKey,对吗?有。我用的是城市照明。你好,伊萨克。我得到这个响应>国家/地区字段不允许过滤。我尝试在CountryResource上添加一个筛选变量,但得到了相同的结果。好的。我解决了,好像没有重启服务器什么的。如果我添加:filtering={“id”:ALL,}它解决了这个问题,你能更新你的答案吗,这样我就可以把它标记为解决方案了?谢谢谢谢Mc-,这句话救了我
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 }