class LinguistResource(ModelResource):

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

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

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

最佳答案

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

关于python - 如何将一些过滤器强制设为好吃的东西?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9428962/

10-12 23:30