Search code examples
pythondjangotastypie

handling multiple query parameters in tastypie


How can I handle parameters like in www.www.ww/api/user=XXX&comment=XXX&friend=XXX style queries(multiple parameters), couldn't found in documentation. (maybe didn't read well)


Solution

  • I've never used tasypi, but you can access the get parameters in django from an HttpRequest object (normally within a view) like so -

    if 'user' in request.GET:
        user = request.GET['user']
    

    Have a look a the django docs on request and response objects. You might even find it handy to check the docs on writing views. A basic view (as apposed to a class based view) uses the HttpRequest as an argument to the function. So

    def my_view(request):
        if 'user' in request.GET:
            user = request.GET['user']
        if 'comment' in request.GET:
            comment = request.GET['comment']
    

    EDIT

    A glance at the Tastypi docs suggests that you should be using the class Meta on your Resource to set this up. Something like -

    class MyResource(ModelResource):
        class Meta:
            filtering = {
                "user": ('exact',),
                "friend": ('exact',),
                "comment": ('exact',)
            }
    

    I believe you can also get at the GET parameters through bundle.request.GET if that's any use.