views:

54

answers:

3

When an update/create is performed on a Django model (.save()) I would like to be able to "step in" and compare some particular attributes to what they were set to previously (if they previously existed at all).

I'm thinking Pre-Save Signals, with a look-up to the original model doing a .objects.get(instance.id), but that feels wasteful. Also, has the validation already happened in pre_save()?

+2  A: 

about model validation :

Note that full_clean() will not be called automatically when you call your model’s save() method

Then, about the pre-save signal, note that you get the instance that is being saved sent as a parameter with the message. As the former version of your model exists only in the database, I don't see where else you could get the previous value of the attributes ...

You don't tell why you want to do this so it's hard to say, but other solutions I'm thinking of right now :

* defining a custom signal that is sent everytime the attributes you are interested in are modified... This signal would then send two arguments : new value, old value
* perform the check directly when setting the attributes

If you give more details, it might be easier...

EDIT :

That's right ... If you emit a custom 'foo_has_updated', you will not be sure that the modification is saved.

In this case, I guess you could cache the variables that interest you while initializing the instance, and catch the post-save OR pre-save signal.

* With pre-save, you would be able to pre-process the data, but the saving operation might fail
* With post-save, you would be sure that the data has been saved.

Caching your variables could be done like this :

class CachedModel(models.Model):
    cached_vars = [var1, var2, varN]
    def __init__(self, *args, **kwargs):
        super(CachedModel, self).__init__(*args, **kwargs)
        self.var_cache = {}
        for var in self.cached_vars:
            self.var_cache[var] = copy.copy(getattr(self, var))

Or something like this ... Then, in your signal handler :

def post_save_handler(sender, **kwargs):
    instance = kwargs["instance"]
    [(instance.var_cache[var], getattr(instance, var)) for var in instance.cached_var]
    #[(<initial value>, <saved value>)

And you got what you needed (I think)!!!

sebpiq
Thanks for your answer. Specifically, and yet quite generically, I'd like to know when a particular field (a choicefield, for example) has been changed. Going further, I'd like to do just as you said - emit a custom signal when this happens. Thus going back to my question - when a model is updated, how can I compare it's previous values to send this signal? (If the object isn't guaranteed to be saved before post_save, it doesn't seem right to emit a foo_has_updated signal, without verifying... but that's another question altogether).
anonymous coward
Edited !!!!!!!!
sebpiq
A: 

You can ask for the currently stored values in the database before you save. I have done this to log only changed values, but it causes another database query each time you save.

Matthew Schinckel
A: 

While I very much approve of Sébastien Piquemal's answer I ultimately ended up using both the pre_save and post_save signals. Instead of overriding __init__(), I do something very similar in pre_save, and then check/compare the values in post_save and emit a custom signal from there if certain conditions are met.

I think I'll still accept his answer, for the time spent on it. I don't see where we're doing much different, except I'm doing my work in a signal and he's doing it during initialization.

anonymous coward