dixon
dixon

Reputation: 1285

How to make some filters mandatory in tastypie?

class LinguistResource(ModelResource):

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

Is it possible to make "language" filter mandatory?

My goal is raise error if in GET parameters absent key "language"

Upvotes: 9

Views: 879

Answers (1)

JamesO
JamesO

Reputation: 25936

You can catch this by overriding 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)

Upvotes: 13

Related Questions