Django-ROA (Resource Oriented Architecture)
Use Django's ORM to model remote API resources.
Not very clear? A picture is worth a thousand words:
In this diagram, the repository can be whatever you want, the only requirement is that it handles standard HTTP requests. As a developer all you have to do next is to use Django's ORM in your Django application (connectors are part of Django-ROA).
If you know how ActiveRecord works (from RoR), that's very close to it.
What problems does it solve?
Here are some ideas:
- data handled by a data warehouse (SOA)
- multiple database (as you can customize URLs per model)
- schema-less backend (e.g. CouchDB speaks HTTP)
- access a remote API with your own models (and with django's built-in admin!)
- plug it on top of an RDF triple store (probably my next pony)
- your crazy use-case? ;-)
A basic Twitter example
The complete example is available in source code examples. First let's define some models with URLs from Twitter's API:
class User(Model): name = models.CharField(max_length=50) screen_name = models.CharField(max_length=50) description = models.TextField() @staticmethod def get_resource_url_list(): return u'http://twitter.com/users/show.json' class Tweet(Model): text = models.TextField() source = models.CharField(max_length=50) user = models.ForeignKey(User) @staticmethod def get_resource_url_list(): return u'http://twitter.com/statuses/public_timeline.json'
Now we can launch a Django shell and test our models:
$ python manage.py shell >>> from twitter_roa.models import User, Tweet >>> tweets = Tweet.objects.all() >>> atweet = tweets >>> print atweet.text Uhm can I get a boyfriend with muscles and no bullshit. Yumm. Xoxo >>> print atweet.user.name Jacob Maldonado
It works! But even more interesting, you can use your lovely Django's built-in admin with your new models:
Now you'll even be able to clic on users to go to the Django's admin view. And so on.
You can find me in #firstname.lastname@example.org as david`bgk or through my website.
Go to the issue tracker, verify you request doesn't exist and then submit it.
Use the power of Mercurial Queues and submit your patch(es). Help is really appreciated :-)