【问题标题】:Filtering Error: Lookups are not allowed more than one level deep过滤错误:不允许超过一级的查找
【发布时间】:2014-02-28 02:11:15
【问题描述】:

看看其他人在做什么,这应该是可行的,但我说错了:

“照片”字段的查找深度不得超过一级。

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

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

【问题讨论】:

    标签: django tastypie


    【解决方案1】:

    根据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
            }
    

    【讨论】:

    • 我已经尝试过了,但后来我收到一条错误消息,提示“在字段列表中找不到 photo_set”,它会列出我的照片表中的字段。
    • @Marcin 您的 PhotoResource 模型中的哪个字段是 ManyToMany 字段?现在,您的 ToManyField (which is the attribute argument) 中的第二个参数设置为“photo_set”。这意味着它将 PhotoResource().photo_set 视为 M2M 字段。
    • 感谢 Michael B。在发表评论后,我设法让它工作了! :)
    猜你喜欢
    • 2021-09-10
    • 2014-07-05
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    相关资源
    最近更新 更多