After a few hours of coding I was happy, I thought I had restored all the data. But some relationships were still missing, the problem was that some objects were not imported as they already existed, so the relationships were not restored from the serialized data.
Unfortunately Django's DeserializedObject isn't well documented, I had to check out the Django source code to solve the problem. Instances of that class have an m2m_data property that is a dictionary that contains the relationship information.
for deserialized_object in serializers.deserialize("json", data): db_object = models.Foo.objects.get(id=deserialized_object.id) db_object.related_class.set(deserialized_object.m2m_data["related_class"])
In my case the related objects still had the same IDs after being recreated, if that's not the case things would become more complicated.