过滤错误:查找不允许超过一个级别

时间:2014-02-28 02:11:16

标签: django tastypie

从四处看看其他人在做什么这应该有效,但我错误地说:

  

“照片”字段中的查找不允许超过一个级别。

这是我的代码。我尝试了一些细微的变化,但是我没有运气。

class CollectionResource(ModelResource):
    photos = fields.ToManyField('photoproject.apps.kit.api.PhotoResource', 'photo_set', null=True, full=True)

    class Meta:
        authorization = Authorization()
        resource_name = 'collection'

        queryset = Collection.objects.all()

        filtering = {
            'name': ['exact'],
            'photos': ALL
        }

class PhotoResource(ModelResource):
    collection = fields.ToOneField(CollectionResource, 'collection')

    class Meta:
        authorization = Authorization()
        resource_name = 'photo'

        queryset = Photo.objects.all()

        filtering = {
            'id': ALL_WITH_RELATIONS
        }

我想要查询的是:

/api/v1/collection/?photos__id=2

1 个答案:

答案 0 :(得分:4)

根据此StackOverflow answer,尝试将ALL更改为ALL_WITH_RELATIONS以获取“照片”:

class CollectionResource(ModelResource):
    photos = fields.ToManyField('photoproject.apps.kit.api.PhotoResource', 'photo_set', null=True, full=True)

    class Meta:
        authorization = Authorization()
        resource_name = 'collection'

        queryset = Collection.objects.all()

        filtering = {
            'name': ['exact'],
            'photos': ALL_WITH_RELATIONS
        }