title: Copy fields from one instance to another in Django
body: <p>I have the following code which takes an existing instance and copies, or 'archives' it, in another model and then deletes it replacing it with the draft copy. </p>
  
  <p><strong><em>Current Code</em></strong></p>
  
  <pre><code>def archive_calc(self, rev_num, primary_field):
      model_a = Calc.objects.get(tag_number__tag_number = primary_field, revision_number = rev_num) #Current Revision instance
      model_b = CalcArchive() #Draft instance
  
      #Copies data to archive model
      for field in model_a._meta.fields:
          setattr(model_b, field.name, getattr(model_a, field.name))
  
      model_b.pk = None
      model_b.current_revision = False
      model_b.save()
  
      model_a.delete()
  </code></pre>
  
  <p>This works fine however i need to change the system to allow for certain models with foreign keys as when an instance is archived/deleted the related records are deleted along with it. So my idea to fix this is to have the changes from the draft record copied to the previous record and then have the draft deleted thus maintaining the foreign key related records.</p>
  
  <p><strong><em>Solution idea</em></strong></p>
  
  <pre><code>def archive_calc(self, rev_num, primary_field):
      model_a = Calc.objects.get(tag_number__tag_number = primary_field, revision_number = rev_num) #Current Revision instance
      model_b = CalcArchive() #Archive Instance
      model_c = Calc.objects.get(pk = self.object.pk) #Draft instance
  
      #Copies data to archive model
      for field in model_a._meta.fields:
          setattr(model_b, field.name, getattr(model_a, field.name))
  
      model_b.pk = None
      model_b.current_revision = False
      model_b.save()
  
      #Copies data from draft instance to current revision instance
      for field in model_c._meta.fields:
          setattr(model_a, field.name, getattr(model_c, field.name))
  
      model_c.delete()
  </code></pre>
  
  <p>Unfortunately the above solution doesn't work, it just seems to ignore the copy and continues to work as per 'Current Code'. If I add <code>model_a.save()</code> after  <code>for field in model_c._meta.fi...</code> the system gets stuck in a loop and eventually throws <code>maximum recursion depth exceeded in cmp</code>.</p>
  
  <p>Any help would be much appreciate as usual and if im barking up the wrong tree please let me know.</p>