[Django]-Django deep serialization – follow reverse foreign key constraints

0👍

So, here’s a super budget way of doing this which works for my purposes, but I feel like there has to be a better way (including it here in case others are looking for how to do this). It works for me only because I’m sending just one Company object at a time, and as such the fact that it doesn’t explicitly preserve the relationship hierarchy isn’t a big deal.

Given Company instance of “company”:

companyJSON = serializers.serialize('json', [company, ])
employeeJSON = serializers.serialize('json', company.employee_set.all())
fullJSON = companyJSON[:-1] + ", " + employeeJSON[1:]
👤CQP

Leave a comment