1👍
This might help you out: http://wadofstuff.blogspot.com/2009/02/django-full-serializers-part-i.html
0👍
django-tastypie
will do the trick. It has all kinds of support for deep relations like that, as well as adhering to REST, meaning if you use jQuery, a simple $.ajax()
will do the trick to get the data.
Because tastypie adheres to REST, it also has support for updates, inserts, and deletes using PUT
, POST
, and DELETE
methods, respectively.
It supports JSON, XML, and YAML as well. It helps build out a full REST API, which might seem a bit obtuse for what you’re trying to do, but it’s pretty easy to get set up, and lets you fully customize what fields get returned, and which fields are excluded.
In your API, you would do something like:
from tastypie.resources import Resource
from django.contrib.auth.models import User
from myapp import models
class UserResource(Resource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
class EventResource(Resource):
user = fields.ToOneField(UserResource, full=True)
class Meta:
queryset = models.Event.objects.all()
resource_name = 'event'
This won’t come back formatted exactly as you specified, but it is easily configured, and adheres to a web standard, which becomes markedly more useful as your project grows.