I’ve just started using Tastypie and am trying to work out how to format the output as I would like (I’m only interested in GET methods).
I have a quiz object which may have multiple questions, each question may be in multiple quizzes (hence the many to many, rather than one to many) – but when someone requests the quiz object via the API, I only want to return a (json) array of the questions, I don’t want/need to display the intermediate relationship table data.
Some code:
Models.py:
class Question(models.Model):
owner = models.ForeignKey(User)
created_date = models.DateTimeField('date created',default=datetime.now)
lastupdated_date = models.DateTimeField('date updated',default=datetime.now)
title = models.CharField(max_length=500)
def __unicode__(self):
return self.title
class Quiz(models.Model):
owner = models.ForeignKey(User)
created_date = models.DateTimeField('date created',default=datetime.now)
lastupdated_date = models.DateTimeField('date updated',default=datetime.now)
title = models.CharField(max_length=200)
description = models.TextField(blank=True)
props = models.TextField(blank=True)
questions = models.ManyToManyField(Question, through='QuizQuestion')
def __unicode__(self):
return self.title
class QuizQuestion(models.Model):
quiz = models.ForeignKey(Quiz)
question = models.ForeignKey(Question)
order = models.IntegerField(default=1)
and resources.py:
class QuizResource(ModelResource):
q = fields.ToManyField('mquiz.api.resources.QuizQuestionResource', 'quizquestion_set', related_name='quiz', full=True)
class Meta:
queryset = Quiz.objects.all()
allowed_methods = ['get']
fields = ['title', 'id']
resource_name = 'quiz'
include_resource_uri = False
class QuizQuestionResource(ModelResource):
question = fields.ToOneField('mquiz.api.resources.QuestionResource', 'question', full=True)
class Meta:
queryset = QuizQuestion.objects.all()
allowed_methods = ['get']
include_resource_uri = False
class QuestionResource(ModelResource):
class Meta:
queryset = Question.objects.all()
allowed_methods = ['get']
fields = ['title']
resource_name = 'question'
include_resource_uri = False
This functions well enough, but doesn’t quite give the output I’d like. It gives me the output:
{
"id": "1",
"q": [
{
"id": "1",
"order": 1,
"question": {
"title": "What is the capital of Latvia?"
}
},
{
"id": "2",
"order": 2,
"question": {
"title": "What is the capital of Ethiopia?"
}
}
],
"title": "Capitals"
}
However, what I would really like is the output in this format, since I don’t need to have all the intermediate table id/order fields showing:
{
"id": "1",
"q": [
{
"title": "What is the capital of Latvia?"
},
{
"title": "What is the capital of Ethiopia?"
}
],
"title": "Capitals"
}
Is there a way to achieve this? Any help/pointers much appreciated.
UPDATE:
Using a custom serialiser like this seems to work:
class QuizJSONSerializer(Serializer):
json_indent = 2
def to_json(self, data, options=None):
options = options or {}
data = self.to_simple(data, options)
for question in data['q']:
del question['id']
del question['order']
for qkey, qvalue in question['question'].items():
question[qkey] = qvalue
del question['question']
return simplejson.dumps(data, cls=json.DjangoJSONEncoder,
sort_keys=True, ensure_ascii=False, indent=self.json_indent)
Sure there may be more generic way to code this – but works for now.
Overriding the serialize function of the resource object with your own code would be a good approach, but refactoring the data bundle within the dehydrate method would be a quick fix