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.
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) #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()
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.
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) #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()
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 model_a.save()
after for field in model_c._meta.fi...
the system gets stuck in a loop and eventually throws maximum recursion depth exceeded in cmp
.
Any help would be much appreciate as usual and if im barking up the wrong tree please let me know.