Guest User

Untitled

a guest
May 27th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. def copy_model_instance(obj):
  2. initial = dict([(f.name, getattr(obj, f.name))
  3. for f in obj._meta.fields
  4. if not isinstance(f, AutoField) and\
  5. not f in obj._meta.parents.values()])
  6. return obj.__class__(**initial)
  7.  
  8.  
  9. def make_tree_copy(question, parent=None):
  10.  
  11. if parent:
  12. parent = Question.objects.get(id=parent.id)
  13.  
  14. new_question = copy_model_instance(question)
  15. new_question.save()
  16. new_question.move_to(parent)
  17.  
  18. for child in question.get_children():
  19. make_tree_copy(child, new_question)
Add Comment
Please, Sign In to add comment