Tutorial :Remote access to Django ORM


Maybe I should first give an idea of what I want to accomplish as there might be a much better solution.

I have a web application using Django that manages media (Recorded TV, movies, etc). The web app allows you to add meta data to the media such as what you have watched on a per user basis and allows you to perform searches and synchronizes with web resources to get information on show times and so on.

The actual media files lives on a file server, to which the Django system has access to scan and update its database.

I also have several media playing systems that can play the media files from the fileserver, with the front-ends written in Python.

This frontend needs the meta data to display a user interface. Currently I'm creating views with Django that present the data in XML format and then retrieve the pages on the front-ends using urllib2.

What I would really like is a way to access the Django ORM that manages the media meta data from the media playback systems directly so I can have the power the full ORM there.

So, finally back to my question. Is there a way to remotely access the ORM in a standalone fashion? I guess I could copy the model to the front-end systems and then give them all direct access to the database, but there must be a more elegant solution.


Do you really need the full power of ORM in the client? If you don't, some kind of Web service would be favorite for my money. REST is the current darling, see e.g. django-rest-interface.

Otherwise I don't see an elegant way out. You could maybe escape the need for direct DB connections by pickling Querysets and the results and tossing them around via some API, but it's probably more hassle than it's worth.


I don't think accessing the database directly is a bad way to achieve this. If you put your Models into a suitable module, sharing them should not be a problem.

However, you expose the Model definitions, so it may be a bit difficult to change you database schema and model definitions. So using XML as an interface to decouple this is not really bad.

Another option would be some kind of remote procedure call mechanism.


For my own reference (and perhaps others), here is a quick solution and example using the following libraries:

I know that Python 2.6 has built-in support for JSON, but unfortunately I'm stuck with Python 2.5 due to other library restrictions, at least for now.

Django model:

class Show(models.Model):      name = models.CharField(max_length=128)  

Django urls.py:

from django_restapi.model_resource import Collection  from django_restapi.responder import JSONResponder    from wstest.tv.models import Show    show_resource = Collection(          queryset = Show.objects.all(),          responder = JSONResponder(),          )  ##  ##    urlpatterns = patterns('',      ##      (r'^json/show/(.*?)/?$', show_resource),  )  

And then on the client side to read the list of shows (interactive example):

>>> from restful_lib import Connection  >>> import json  >>> # Create restful connection object  >>> conn = Connection("http://localhost:8000")  >>> # Use a get request to get a list of shows  >>> res = conn.request_get("/json/show/")  >>> # Convert the body of the response to a python object  >>> shows=json.read(str(res['body']))  >>> # result  >>> shows  [{'pk': 1, 'model': 'tv.show', 'fields': {'name': 'The Big Bang Theory'}}, {'pk': 2, 'model': 'tv.show', 'fields': {'name': 'Stargate Atlantis'}}, {'pk': 3, 'model': 'tv.show', 'fields': {'name': 'Fringe'}}, {'pk': 4, 'model': 'tv.show', 'fields': {'name': 'CSI Las Vegas'}}]  

Thanks for pointing me in the right direction everyone.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »