Priyeshj
Priyeshj

Reputation: 1345

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

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.

Upvotes: 20

Views: 16016

Answers (2)

Marcin
Marcin

Reputation: 49846

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.

Upvotes: 30

Alexey Savanovich
Alexey Savanovich

Reputation: 1903

I think this happens because survey assigned to survey_new on this line of code

survey_new = survey

An then when survey_new saved

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()

survey became equal to survey_new. For example you can check it so

id(survey)
# 50016784
id(survey_new)
# 50016784

or Django equivalent

survey.id
# 10
survey_new.id
# 10

To figure out the issue all required objects have to be collected before assignment

survey_section = survey.sections.all().order_by('order')
# ... all other questions and options 

survey_new = survey
survey_new.title = survey.title + ' -- Copy'
survey_new.owner = str(new_owner_id)
# ... your remaining code

Upvotes: 1

Related Questions