python - Duplicate/Archive entry in Django to another model -
i have been @ 2 days , i'm hoping can point me in right direction. trying duplicate entry in table/model model mirrored fields, creating archived version. want happen when user calls update view.
what have tried far setting pk
none
, trying find way move previous version mirrored/archive model. after couple of hours of research gave on path. next thought answer lie pre_save
receiver can't find way access model instance save
archive model.
@receiver(pre_save, sender=instrumentannual) def archive_calc_instance(sender, instance, **kwargs): stored_id = getattr(instance, 'id', none) e = instrumentannual.objects.filter(id = stored_id) archive = instrumentannualarchive(e.field_name, e.another_field_name...) archive.save()
as far can tell should work e
contains first field model.
is there can done code achieve goal or there more 'django' way solve this? i.e. sort of official archive feature?
thanks in advance.
with of @igor's comment amended solution this:
def archive_calc(self, object_id): annual = instrumentannual.objects.get(id = object_id) annual_archive = instrumentannualarchive() field in annual._meta.fields: setattr(annual_archive, field.name, getattr(annual, field.name)) annual_archive.pk = none annual_archive.save()
it occured me using pre_save
wouldn't work listening/linked model, not view thought. placed above code in update view , called passing id
in object_id
.
thanks again help.
Comments
Post a Comment