Deserializing Django objects and many to many relationships


I recently had to restore some data from backups, and was using Django serializers to get the job done. Objects of one class were not deleted, but the relationships were set to None when the related objects were deleted. Here's how I restored the relationship information.

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)

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.



Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo