如何在tastypie中强制使用某些过滤器?

时间:2012-02-24 10:15:17

标签: python django rest filtering tastypie

class LinguistResource(ModelResource):

    class Meta:
        model = Linguist
        queryset = Linguist.objects.all()
        resource_name = 'linguists_by_language'
        filtering = {
            "language": ('exact', ),
        }

是否可以强制使用“语言”过滤器?

如果在GET参数中没有键“language”

,我的目标是引发错误

1 个答案:

答案 0 :(得分:13)

您可以通过覆盖build_filters

来捕捉到这一点
from tastypie.exceptions import BadRequest

def build_filters(self, filters=None):
    if 'language' not in filters:
         raise BadRequest("missing language param") # or maybe create your own exception
    return super(LinguistResource, self).build_filters(filters)