Copy Model Object in Django
Photo by Adam Nieścioruk on Unsplash
How to copy a database record in Django
I ran into a situation where I wanted to created a new database record from an
existing record (model object). I figured there should be a fairly simple
solution. It turns out there is and I want to thank Seveas
on IRC #django for
pointing it out for me. This is essentially what I did:
1from copy import deepcopy2old_obj = deepcopy(obj)3old_obj.id = None4old_obj.save()
Voila!