Duplicate django objects using ManyToManyFields

I am using Django and I have some objects with ManyToManyFields. I would like to duplicate these objects. I found "deepcopy" that works almost perfectly.

>>> e = Equipement.objects.get(pk=568) >>> ee = deepcopy(e) >>> ee.connexion.all() [<Connexion: COMETE - Proxyweb>] >>> ee.id=None >>> ee.save() >>> ee.connexion.all() [] 

I do not want to lose ManyToMany information when saving. Do you know the trick to do it fast in Django?

Thanks.

+4
source share
1 answer

Just add them using the old object:

 ee = deepcopy(e) ee.id=None ee.save() ee.connexion.add(*e.connexion.all()) 
+4
source

All Articles