【问题标题】:Why can't I add custom key/value pair to orm_filters in TastyPie Advanced filter definition?为什么我不能在 TastyPie Advanced 过滤器定义中向 orm_filters 添加自定义键/值对?
【发布时间】:2012-07-28 23:39:58
【问题描述】:

按照此处提供的示例:https://stackoverflow.com/a/10022244/1386588 我遇到了与提供的示例代码有关的问题。

我有以下高级过滤器定义:

def build_filters(self, filters=None):
        if filters is None:
            filters = {}

        orm_filters = super(TaggedResource, self).build_filters(filters)

        if('query' in filters):
            query = filters['query']
            print query
            qset = (
                    Q(comment__icontains=query) |
                    Q(media_text__icontains=query)
                    )
            orm_filters.extend({'custom': qset})

我遇到的问题是 orm_filters 是一个字典映射而不是一个列表。因此,我得到了错误:

"error_message": "'dict' object has no attribute 'extend'"

如果我尝试像使用常规 dict 对象一样添加键和值对:

orm_filters['custom'] = qset

在添加“自定义”键后尝试return orm_filters 时出现以下错误:

"error_message": "Cannot resolve keyword 'custom' into field. Choices are: comment, id, media_text, ..."

知道我做错了什么吗?

我一直在努力寻找解决方案,但没有任何运气,在此先感谢您的帮助!

更新:我认为问题在于我尝试覆盖 apply_filters 没有实例化:

def apply_filters(self, request, applicable_filters):
        print 'applied filters'
        if 'custom' in applicable_filters:
            custom = applicable_filters.pop('custom')
        else:
            custom = None

        semi_filtered = super(TaggedResource, self).apply_filters(request, applicable_filters)

        return semi_filtered.filter(custom) if custom else semi_filtered

这可能是我收到无法解析关键字“自定义”错误的原因,因为我希望此处的 apply_filters 方法在从 apply_filters 中获取“自定义”后将其删除。

不确定我的 apply_filters def 有什么问题。

【问题讨论】:

    标签: django tastypie


    【解决方案1】:

    尝试改变:

    orm_filters.extend({'custom': qset})
    

    到:

    orm_filters.update(**qset)
    

    更新:(测试代码)

    def build_filters(self, filters=None):
        filters = filters
    
        applicable_filters = super(TaggedResource, self).build_filters(filters)
    
        query = filters.get('query')
        if query:
            qset = (
                Q(comment__icontains=query) |
                Q(media_text__icontains=query)
            )
            applicable_filters['custom'] = qset
    
        return applicable_filters
    
    def apply_filters(self, request, applicable_filters):
        custom = None
        if 'custom' in applicable_filters:
            custom = applicable_filters.pop('custom')
    
        semi_filtered = super(TaggedResource, self).apply_filters(request, applicable_filters)
    
        return semi_filtered.filter(**custom) if custom else semi_filtered
    

    【讨论】:

    • 我试过你放的东西,但我也需要一把钥匙('custom')。 orm_filters.update(**{'custom':qset}) 使用 Q 对象将“自定义”添加到 dict 作为它的值对,但是当我尝试返回它时它仍然给我与以前相同的错误:"error_message": "Cannot resolve keyword 'custom' into field. Choices are: comment, ..."
    • 当然,您会收到该消息,自定义不是字段,您无法过滤不存在​​的内容,您为什么要这样做?
    • 我认为没关系,因为我只是将它添加到 build_filters 内的映射中。过滤器实际上并未使用“自定义”运行 - As is shown here,在将“自定义”传递给适用的过滤器后,我正在弹出“自定义”。 custom = applicable_filters.pop('custom')
    • CHANGE return semi_filtered.filter(custom) if custom else semi_filtered TO return semi_filtered.filter(**custom) if custom else semi_filtered
    • 我了解您的建议;但是,我认为它没有解决真正的问题,即在请求期间根本没有调用 apply_filters 方法。这可能就是我收到错误 Cannot resolve keyword 'custom' into field. 的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2011-11-12
    • 1970-01-01
    • 2020-09-24
    • 2011-10-17
    相关资源
    最近更新 更多