Solving "Django clone the recursive objects" (https://stackoverflow.com/questions/61584535/django-clone-the-recursive-objects)
Tested with: Python 3.7.6 / Django 3.0.6
Post objects can be cloned as follows:
from .clone_object import clone_object
clone = clone_object(post)
- def clone_object(obj, attrs={}) starts by build a "flat" clone of the given "obj"
- M2M fields are managed by replicating all related records found on parent "obj" into "clone"
- for 1-1 and 1-N relations, we clone all child objects by calling recursively clone_object(), but overridding the remote field with the new cloned object
See clone_object.py