【问题标题】:How to make some filters mandatory in tastypie?如何在美味派中强制使用一些过滤器?
【发布时间】:2012-03-14 19:08:34
【问题描述】:
class LinguistResource(ModelResource):

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

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

如果在 GET 参数中缺少键“语言”,我的目标是引发错误

【问题讨论】:

    标签: python django rest filtering tastypie


    【解决方案1】:

    您可以通过覆盖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)
    

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 2011-10-24
      • 2013-06-01
      • 2015-03-21
      • 2019-09-19
      • 1970-01-01
      • 2014-03-15
      • 2014-04-18
      • 1970-01-01
      相关资源
      最近更新 更多