2012-02-24 27 views
9
class LinguistResource(ModelResource): 

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

Có thể đặt bộ lọc "ngôn ngữ" bắt buộc không?Làm thế nào để làm cho một số bộ lọc bắt buộc trong ngon?

Mục tiêu của tôi là nâng lỗi nếu trong các tham số GET chủ chốt vắng mặt "ngôn ngữ"

Trả lời

13

Bạn có thể bắt này bằng cách ghi đè 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) 
Các vấn đề liên quan