Discussion:
Getting former value of altered field when saving model
Aaron
2009-10-07 17:01:59 UTC
Permalink
I have a model with a particular field I'm interested in. If that
field is altered, I want to be able to access the former value of that
field (as well as the new value).

I've looked at pre_save() signals, models, and fields, and it seems
that I am only able to access the current (new) value of the field;
it's old value is lost.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Nan
2009-10-07 17:14:51 UTC
Permalink
class Foo(models.Model):
bar = models.IntegerField()

def save(self, force_insert=False, force_update=False):
if self.id:
# this is an update rather than a new instance
old = Foo.objects.get(pk=self.id)
# DO STUFF
super(Foo, self).save(force_insert, force_update)
Post by Aaron
I have a model with a particular field I'm interested in. If that
field is altered, I want to be able to access the former value of that
field (as well as the new value).
I've looked at pre_save() signals, models, and fields, and it seems
that I am only able to access the current (new) value of the field;
it's old value is lost.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Aaron
2009-10-07 17:32:18 UTC
Permalink
    bar = models.IntegerField()
            # this is an update rather than a new instance
            old = Foo.objects.get(pk=self.id)
            # DO STUFF
        super(Foo, self).save(force_insert, force_update)
Oh, so I'm actually able to get the old value of the field after all:

def save(self, force_insert=False, force_update=False):
if self.id:
# this is an update rather than a new instance
old = Foo.objects.get(pk=self.id)

old_field = old.field_that_i_want
new_field = self.field_that_i_want

# Do something with old_field and new_field

super(Foo, self).save(force_insert, force_update)

Thanks. :)

Would this also work in pre_save or post_save signals, or only in the
model's save method?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Nan
2009-10-07 18:25:31 UTC
Permalink
It should work in pre_save, but by the time you reach post_save, the
database ID will be associated with the new values.
Post by Nan
Post by Nan
bar = models.IntegerField()
# this is an update rather than a new instance
old = Foo.objects.get(pk=self.id)
# DO STUFF
super(Foo, self).save(force_insert, force_update)
# this is an update rather than a new instance
old = Foo.objects.get(pk=self.id)
old_field = old.field_that_i_want
new_field = self.field_that_i_want
# Do something with old_field and new_field
super(Foo, self).save(force_insert, force_update)
Thanks. :)
Would this also work in pre_save or post_save signals, or only in the
model's save method?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Continue reading on narkive:
Loading...