user1076881
user1076881

Reputation: 251

Django-tastypie REST url that contains filter criteria

I fairly new to Django-Tastypie, I looking at the Getting started example below: http://django-tastypie.readthedocs.org/en/latest/tutorial.html#hooking-up-the-resource-s

  1. http://127.0.0.1:8000/api/entry/?format=json
  2. http://127.0.0.1:8000/api/entry/1/?format=json
  3. http://127.0.0.1:8000/api/entry/schema/?format=json

Would it be possible to allow rest URL which contains filter criteria in certain format, that would be used to filter objects to be returned?

That would mean I have to do something like in this thread: REST urls with tastypie ?

Upvotes: 0

Views: 3688

Answers (1)

kgr
kgr

Reputation: 9948

Yes, Tastypie allows for filtering out of the box, given you're using ModelResource as the base clas for your Resources. You just need to declare which attributes can be filtered on and then you're good to go.

For example:

#resource definition
class MyResource(ModelResource):
    class Meta:
        filtering = {
            "slug": ('exact', 'startswith',),
            "title": ALL,
        }

# the request
GET /api/v1/myresource/?slug=myslug

see Tastypie documentation for more details.

Upvotes: 6

Related Questions