Using DjangoFullSerializers to serialize custom ManyToMany relations into JSON
I'm using DjangoFullSerializers to serialize this model (into JSON):
class Program(models.Model):
name = models.CharField(...)
start_date = models.DateField(...)
timelin开发者_如何转开发e = models.PositiveIntegerField(...)
complete = models.BooleanField(...)
designers = models.ManyToManyField(Designer, through='ProgramAssignment')
milestones = models.ManyToManyField(Milestone, through='ProgramMilestone')
The problem is that I would like to be able to serialize all of this information -- i.e. not just the first four fields but all the designer & milestone information linked to this program. I've also applied a patch which seems to be able to solve my problem, but I've tried many different values for the relations
keyword argument, such as 'milestones'
, 'milestone_set'
, 'ProgramMilestones'
, 'ProgramMilestone_set'
, etc. and none of them have worked so far for me.
If anyone knows how to serialize the last two M2M fields (using DjangoFullSerializers or otherwise), please let me know. Thanks!
I got it. The problem was that I needed to be calling Program.objects.select_related() and then pass the keyword argument relations=('programmilestone_set', 'programassignment_set',)
.
精彩评论