How can I create a deep clone of a DB object in Django?

Priyeshj picture Priyeshj · Jan 18, 2012 · Viewed 9.8k times · Source

I am trying to create a complete copy of a survey instance, which has several sections, and each section has several questions and finally each question has several options. I am using standard django 1.3.1, with MySQL. I need to be able to create a complete copy of all these elements, for a different survey owner. What I currently have in the view is:

    survey_new = survey
    survey_new.title = survey.title + ' -- Copy'
    survey_new.owner = str(new_owner_id)
    survey_new.created = datetime.now()
    survey_new.pk = None
    survey_new.save()

    for sec in survey.sections.all().order_by('order'):
        sec_n = sec
        sec_n.survey_id = survey_new.id 
        sec_n.pk = None
        sec_n.save()

        for q in sec.questions.all().order_by('order'):
            q_n = q
            q_n.section_id = sec_n.id
            q_n.pk = None
            q_n.save()

            for op in q.options.all().order_by('order'):
                op_n = op
                op_n.question_id = q_n.id
                op_n.pk = None
                op_n.save()

However, this seems to run through all the loops without any errors, and just creating a copy of survey. I was hoping that this would copy the survey, sections, questions, options for that survey instance. Just can't seem to figure out what I am doing wrong here.

Answer

Marcin picture Marcin · Jan 19, 2012

Googling "django deep copy" returns this on the first page: Copy Model Object in Django | Nerdy Dork

The code sample given is:

from copy import deepcopy
old_obj = deepcopy(obj)
old_obj.id = None
old_obj.save()

If I understand your question correctly, you will want to change some more fields before saving.