Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:12:25+00:00 2026-05-11T03:12:25+00:00

I’ve models for Books , Chapters and Pages . They are all written by

  • 0

I’ve models for Books, Chapters and Pages. They are all written by a User:

from django.db import models  class Book(models.Model)     author = models.ForeignKey('auth.User')  class Chapter(models.Model)     author = models.ForeignKey('auth.User')     book = models.ForeignKey(Book)  class Page(models.Model)     author = models.ForeignKey('auth.User')     book = models.ForeignKey(Book)     chapter = models.ForeignKey(Chapter) 

What I’d like to do is duplicate an existing Book and update it’s User to someone else. The wrinkle is I would also like to duplicate all related model instances to the Book – all it’s Chapters and Pages as well!

Things get really tricky when look at a Page – not only will the new Pages need to have their author field updated but they will also need to point to the new Chapter objects!

Does Django support an out of the box way of doing this? What would a generic algorithm for duplicating a model look like?

Cheers,

John


Update:

The classes given above are just an example to illustrate the problem I’m having!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:12:25+00:00Added an answer on May 11, 2026 at 3:12 am

    This no longer works in Django 1.3 as CollectedObjects was removed. See changeset 14507

    I posted my solution on Django Snippets. It’s based heavily on the django.db.models.query.CollectedObject code used for deleting objects:

    from django.db.models.query import CollectedObjects from django.db.models.fields.related import ForeignKey  def duplicate(obj, value, field):     """     Duplicate all related objects of `obj` setting     `field` to `value`. If one of the duplicate     objects has an FK to another duplicate object     update that as well. Return the duplicate copy     of `obj`.       """     collected_objs = CollectedObjects()     obj._collect_sub_objects(collected_objs)     related_models = collected_objs.keys()     root_obj = None     # Traverse the related models in reverse deletion order.         for model in reversed(related_models):         # Find all FKs on `model` that point to a `related_model`.         fks = []         for f in model._meta.fields:             if isinstance(f, ForeignKey) and f.rel.to in related_models:                 fks.append(f)         # Replace each `sub_obj` with a duplicate.         sub_obj = collected_objs[model]         for pk_val, obj in sub_obj.iteritems():             for fk in fks:                 fk_value = getattr(obj, "%s_id" % fk.name)                 # If this FK has been duplicated then point to the duplicate.                 if fk_value in collected_objs[fk.rel.to]:                     dupe_obj = collected_objs[fk.rel.to][fk_value]                     setattr(obj, fk.name, dupe_obj)             # Duplicate the object and save it.             obj.id = None             setattr(obj, field, value)             obj.save()             if root_obj is None:                 root_obj = obj     return root_obj 

    For django >= 2 there should be some minimal changes. so the output will be like this:

    def duplicate(obj, value=None, field=None, duplicate_order=None):     """     Duplicate all related objects of obj setting     field to value. If one of the duplicate     objects has an FK to another duplicate object     update that as well. Return the duplicate copy     of obj.     duplicate_order is a list of models which specify how     the duplicate objects are saved. For complex objects     this can matter. Check to save if objects are being     saved correctly and if not just pass in related objects     in the order that they should be saved.     """     from django.db.models.deletion import Collector     from django.db.models.fields.related import ForeignKey      collector = Collector(using='default')     collector.collect([obj])     collector.sort()     related_models = collector.data.keys()     data_snapshot = {}     for key in collector.data.keys():         data_snapshot.update(             {key: dict(zip([item.pk for item in collector.data[key]], [item for item in collector.data[key]]))})     root_obj = None      # Sometimes it's good enough just to save in reverse deletion order.     if duplicate_order is None:         duplicate_order = reversed(related_models)      for model in duplicate_order:         # Find all FKs on model that point to a related_model.         fks = []         for f in model._meta.fields:             if isinstance(f, ForeignKey) and f.remote_field.related_model in related_models:                 fks.append(f)         # Replace each `sub_obj` with a duplicate.         if model not in collector.data:             continue         sub_objects = collector.data[model]         for obj in sub_objects:             for fk in fks:                 fk_value = getattr(obj, "%s_id" % fk.name)                 # If this FK has been duplicated then point to the duplicate.                 fk_rel_to = data_snapshot[fk.remote_field.related_model]                 if fk_value in fk_rel_to:                     dupe_obj = fk_rel_to[fk_value]                     setattr(obj, fk.name, dupe_obj)             # Duplicate the object and save it.             obj.id = None             if field is not None:                 setattr(obj, field, value)             obj.save()             if root_obj is None:                 root_obj = obj     return root_obj 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.