How it works...

Each endpoint of Tastypie should have a class extending ModelResource defined. Similar to the Django models, the configuration of the resource is set in the Meta class:

  • The queryset parameter defines the QuerySet of objects to list.
  • The resource_name parameter defines the name of the URL endpoint for reverse lookup.
  • The fields parameter lists out the fields of the model that should be shown in
    the API. Alternatively, excludes can be used to blacklist fields instead.
  • The allowed_methods parameter lists the request methods, such as GET, POST, PUT, DELETE, and PATCH (this being the default set). It is also possible to specify a different set of list_allowed_methods for listings, and detail_allowed_methods for individual records.
  • The authentication parameter defines how third parties can authenticate themselves when connecting to the API. The available options are Authentication (default), BasicAuthentication, ApiKeyAuthentication, SessionAuthentication, DigestAuthentication, OAuthAuthentication, MultiAuthentication, or your own custom authentication. In our case, we are using ApiKeyAuthentication as we want each user to use username and api_key.
  • The authorization parameter answers the authorization question: is permission granted to this user to take the stated action? The possible choices are Authorization, ReadOnlyAuthorization, DjangoAuthorization, or your own custom authorization. In our case, we are using ReadOnlyAuthorization, as we only want to allow read access to the users.
  • The filtering parameter defines which fields you can use to filter lists via the URL query parameters. For example, with the current configuration, you can filter the items by titles that contain the word movie: http://127.0.0.1:8000/api/v1/bulletins/format=json&username=admin&api_key=xxx&title__contains=movie.

Also, there is a category foreign key that is defined in BulletinResource with the full=True argument, meaning that the full list of category fields will be shown in the bulletin resource instead of an endpoint link.

Besides JSON, Tastypie allows you to use other formats such as XML, YAML, and bplist.

There is a lot more that you can do with APIs using Tastypie. To find out more details, check the official documentation at http://django-tastypie.readthedocs.org/en/latest/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.143.4