[Answer]-Copying Django model row data before updating

1👍

you would have to query the database to get the original object. instance has the updated object, which is ready to be saved into the database.

class Country(models.Model):
    name = models.CharField(max_length=16)

def make_copy(sender, **kwargs):
    obj = kwargs['instance']
    try:
        orig_obj = Country.objects.get(pk=obj.pk)
    except: #If it is a new object
        orig_obj = None

pre_save.connect(make_copy, sender=Country)

0👍

The previous data only exists in the database; you will need to retrieve it in your handler.

Leave a comment