I have the following code that takes an existing instance and copies or “archives” it in another model, and then deletes it, replacing it with a draft.
Current code
def archive_calc(self, rev_num, primary_field):
model_a = Calc.objects.get(tag_number__tag_number = primary_field, revision_number = rev_num)
model_b = CalcArchive()
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()
This works fine, but I need to change the system so that some models have foreign keys, since when archiving / deleting an instance, related records are deleted along with it. Therefore, my idea to fix this is to make changes to the draft records copied to the previous record, and then delete the project, saving the records associated with the foreign key.
Solution idea
def archive_calc(self, rev_num, primary_field):
model_a = Calc.objects.get(tag_number__tag_number = primary_field, revision_number = rev_num)
model_b = CalcArchive()
model_c = Calc.objects.get(pk = self.object.pk)
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()
for field in model_c._meta.fields:
setattr(model_a, field.name, getattr(model_c, field.name))
model_c.delete()
, , "Current Code". model_a.save() for field in model_c._meta.fi..., , , maximum recursion depth exceeded in cmp.
, , , , .