3

To make a clone of a model in Django, I can use this trick:

object.pk=None;
object.save();

Assume I have models A, AB, and ABC. AB has a foreign key of A, ABC has a foreign key of AB.

Now if I want to make a deep clone of one A object a, I have to do it like this:

newa = deepcopy(a)
newa.pk=None
newa.save()

ab = AB.objects.get(a=a)
newab = deepcopy(ab)
newab.a = newa;
newab.pk=None
newab.save()

abc = ABC.objects.get(ab=ab)
abc.pk=None;
abc.ab=newab;
abc.save()

This is a lot of trouble if there are multiple levels of relationship. Is there a convenient way to do this, like DELETE CASCADE in sql, only for clone?

2
  • 4
    You need to manually copy the "related" stuff, see docs. Commented May 17, 2014 at 5:07
  • 1
    Possible duplicate: stackoverflow.com/questions/437166/… Commented May 17, 2014 at 21:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.