Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 567
dixon Avatar asked Feb 24 '12 10:02

dixon


1 Answers

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)
like image 56
JamesO Avatar answered Nov 13 '22 13:11

JamesO