Django tastypie a lot for many relationships - hiding intermediate relationships in json output

I just started using Tastypie and am trying to figure out how to format the output as I would like (I'm only interested in GET methods). I have a quiz object that can have several questions, each of which can be in several polls (there are many, not one to many from here), but when someone requests the quiz object through the API, I only want to return (json), I do not want / should display data of the table of intermediate relations.

Some codes:

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 works pretty well, but it doesn't quite give the result I need. This gives me the result:

 { "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, I would really like to receive the output in this format, since I do not need to have all the id / order fields of the intermediate table:

 { "id": "1", "q": [ { "title": "What is the capital of Latvia?" }, { "title": "What is the capital of Ethiopia?" } ], "title": "Capitals" } 

Is there any way to achieve this? Any help / pointers are greatly appreciated.

UPDATE: Using a custom serializer like this works:

 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) 

Of course, there may be a more general way to encode this - but it works for now.

+4
source share
1 answer

Overriding the serialization function of a resource object using native code would be a good approach, but refactoring a data packet in a dehydration method would be a quick fix

+3
source

Source: https://habr.com/ru/post/1443113/


All Articles