Using Non-ORM data sources with Tastypie in Django


Recently, I’ve worked with Tastypie which is my first introduction to tastypie. Tastypie itself is wonderful and very easy to work. However, my first catch was that I’ve to use it in a scenario where data source is not django’s ORM. My goal is to expose some API endpoints but the data will come from some other external API. Confusing? Let’s discuss about the implementation not about the wisdom of use cases. If you do not have introduction to the Tastypie, please have a look at their documentation.

Generally, when we create a Tastypie resource, our class inherits ModelResource. ModelResource is actually provides django’s ORM specific accesses as the resource is created on top of Django models. The fact is that, ModelResource is a thin wrapper on top of it’s Resource class and overrides few methods. If we want to use our custom data sources, we will have to override some methods of this Resource class based on our requirements. The methods we have to override are:

  • detail_uri_kwargs
  • get_object_list
  • obj_get_list
  • obj_get
  • obj_create
  • obj_update
  • obj_delete_list
  • obj_delete
  • rollback

However, you won’t need to override all of these if you are exposing read endpoints only.  Now, let’s see how can we override the collection (‘/’) endpoint.

Your endpoint may look like: /api/blogs/?format=json. This endpoint will render a collection of blog as following example.

Usually these data are supposed to come from your local database. However, now let’s consider it will be coming from another repository. To achieve these, we will simply need to implement our own obj_get_list in our Resource class (our resource class which inherits tastypie’s Resource).

Among the above code, the method ‘object_get_list’ is the actual implementation of our custom data source. Here I’ve put some static contents. You can write your own data access logics here and at the end return a list of objects. You have probably also noticed, as I’ve created dictionary there, I had to convert them to object so that tastypie can access them correctly.

Read more about using Non ORM data source.

  • Gus

    Nice simple example. Unfortunately, it doesn’t run for me. I’ve pasted your two classes into my module and included the following in my

    v1_api = Api(api_name=’v1′)

    urlpatterns = patterns(”,
    (r’^api/’, include(v1_api.urls)),

    Now when I hit /api/blogs/?format=json I get the following error:

    {“error_message”: “‘NoneType’ object is not callable”, “traceback”: “Traceback (most recent call last):nn File “/home/gus/djangostack-1.4.3-0/python/lib/python2.7/site-packages/django_tastypie-0.9.12-py2.7.egg/tastypie/”, line 202, in wrappern response = callback(request, *args, **kwargs)nn File “/home/gus/djangostack-1.4.3-0/python/lib/python2.7/site-packages/django_tastypie-0.9.12-py2.7.egg/tastypie/”, line 439, in dispatch_listn return self.dispatch(‘list’, request, **kwargs)nn File “/home/gus/djangostack-1.4.3-0/python/lib/python2.7/site-packages/django_tastypie-0.9.12-py2.7.egg/tastypie/”, line 471, in dispatchn response = method(request, **kwargs)nn File “/home/gus/djangostack-1.4.3-0/python/lib/python2.7/site-packages/django_tastypie-0.9.12-py2.7.egg/tastypie/”, line 1258, in get_listn base_bundle = self.build_bundle(request=request)nn File “/home/gus/djangostack-1.4.3-0/python/lib/python2.7/site-packages/django_tastypie-0.9.12-py2.7.egg/tastypie/”, line 690, in build_bundlen obj = self._meta.object_class()nnTypeError: ‘NoneType’ object is not callablen”}

    I’d really like to get this example going. Any ideas as to what the problem may be?

  • can you please put your code in a gist?

  • euclid

    This is also a problem for me.

  • which version of tastypie are you using? I’ve noticed a change in the latest version. The second parameter in ‘obj_get_list’ is not ‘request’ anymore. now it is ‘bundle’. to access the request object from bundle, you have to use ‘bundle.request’

  • Prawyn

    I am also facing the same issue.
    “NoneType’ object is not callable” .
    Did you guys solve this problem?

  • zeenfaiz

    1.replace second arguement as bundle

    2.add object_class=dic2obj under Meta()

    3.modify dic2obj like riakobj() in

  • NoneType problems?

    Read this article 😉

    If youre not using Django then probably you will not need to specify the

    “queryset” on the Meta subclass. In this case you must use “object_class”
    property in a Resource type class

    Hope this helps


  • Aditya Darmawan

    Very easy to understand. Thank you very much. It is even easier than Tastypie’s official doc!

  • mikestoddart

    This is great thanks – my response doesn’t include a resource_uri for my custom resource. How do I generate one?

    can_join: false,
    can_leave: true,
    community: “/community/api/v1/community/5/”,
    resource_uri: “”

  • Ramesh Ravi

    this for getting data…how do i save data to multiple models??