|
|
|
|
|
by alecperkins
4923 days ago
|
|
I like to take this one step further and return referenced objects with both their ID and their URL as actual objects instead of strings. This is incredibly helpful for working with the API in something like Backbone.js, because then there's no need for logic in determining if it is getting a list of IDs as strings or full objects. It's doubly helpful if the references are to objects of different types. The client simply loads the objects as given, and can do a sync if it's missing attributes it expects. (Of course, sending the full data in one request is usually preferable.) Also, the `url` property on the model just returns the `url` attribute instead of needing to duplicate the URL construction pattern. So, …
{
'id': 123,
'url': '/resource/123'
},
…
instead of …
'/resource/123',
'/resource/124',
…
or worse …
123,
124,
…
Also, Django REST Framework is easily my favorite REST API tool for Django. It's very straightforward to use just as much or just as little of it as necessary. In fact, it's powering the main views of the upcoming second draft of http://marquee.by (the entire site is effectively a browsable API). |
|