Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For my own reference (and perhaps others), here is a quick solution and example using the following libraries:</p> <ul> <li><a href="http://code.google.com/p/django-rest-interface/" rel="nofollow noreferrer">django-rest-interface</a> on the server side (as suggested by oggy)</li> <li><a href="http://code.google.com/p/python-rest-client/" rel="nofollow noreferrer">python-rest-client</a> on the client side to do the connection and requests</li> <li><a href="http://pypi.python.org/pypi/python-json/" rel="nofollow noreferrer">python-json</a> on the client side to convert the response to a python object</li> </ul> <p>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.</p> <p>Django model:</p> <pre><code>class Show(models.Model): name = models.CharField(max_length=128) </code></pre> <p>Django urls.py:</p> <pre><code>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), ) </code></pre> <p>And then on the client side to read the list of shows (interactive example):</p> <pre><code>&gt;&gt;&gt; from restful_lib import Connection &gt;&gt;&gt; import json &gt;&gt;&gt; # Create restful connection object &gt;&gt;&gt; conn = Connection("http://localhost:8000") &gt;&gt;&gt; # Use a get request to get a list of shows &gt;&gt;&gt; res = conn.request_get("/json/show/") &gt;&gt;&gt; # Convert the body of the response to a python object &gt;&gt;&gt; shows=json.read(str(res['body'])) &gt;&gt;&gt; # result &gt;&gt;&gt; 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'}}] </code></pre> <p>Thanks for pointing me in the right direction everyone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload