Duplicate django objects with ManyToManyFields
Asked Answered
N

1

6

I use Django and I have some objects with ManyToManyFields. I'd like to duplicate these objects. I've found 'deepcopy' which 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 don't want to loose the ManyToMany information when I save. Do you know a trick in order to do that quickly in Django ?

Thanks.

Norther answered 14/6, 2011 at 16:9 Comment(0)
M
6

Just add them using the old object:

ee = deepcopy(e)
ee.id=None
ee.save()
ee.connexion.add(*e.connexion.all())
Marozas answered 14/6, 2011 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.