There are a couple ways to do this. You can either override the model
methods like save and init, or you can use signals and tie into the
pre_save, post_save, etc. Signals are here:
http://docs.djangoproject.com/en/dev/ref/signals/
If using signals, you will have an instance of the object being saved.
Im sure there are better ways to do it, but i'd probably use the
instance variable and do a query for the original. So query for the
sender where pk=instance.pk, and compare it to the instance being
saved. Again, seems sloppy to me, im sure theres a better way.
If you're overriding the model save, you can create a copy of what you
want to compare in the init function. I just did this the other day.
For example, i wanted to check for a different title. Look at my
overridden __init__ and my overridden save and see how I copied the
title to a new variable for comparison.
class Entry(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
body = models.TextField()
user = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add=True)
def __init__(self, *args, **kwargs):
super(Entry, self).__init__(*args, **kwargs)
#set current title to check for title change on save
self.current_title = self.title
def save(self, *args, **kwargs):
if self.pk: #this save is an update, not a create
if self.title != self.current_title: #new title?
#new title, update slug
self.slug = self.create_unique_slug()
else:
#created new entry, generate slug
self.slug = self.create_unique_slug()
super(Entry, self).save(*args, **kwargs)
On Apr 26, 8:33 am, Derek <[email protected]> wrote:
> The Django docs point to the "hooks" you can use for custom code when
> saving/deleting objects in the Django
> adminhttp://www.djangoproject.com/documentation/models/save_delete_hooks/
>
> What is not clear is how (or rather, where) to create a copy of an object
> before editing, so that it can be compared to the "revised" version altered
> by the user and appropriate code be executed.
>
> How and where can this be done?
>
> Thanks
> Derek
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group
> athttp://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.