To avoid extra DB lookup, I modified constructor to remember initial value and use this in save method later.
To avoid extra DB lookup, I modified constructor to remember initial value and use this in save method later: class Package(models. Model): feedback = models. IntegerField(default = 0, choices = FEEDBACK_CHOICES) feedback_time = models.
DateTimeField(null = True) def __init__(self, *args, **kw): super(Package, self). __init__(*args, **kw) self. _old_feedback = self.
Feedback def save(self, force_insert=False, force_update=False, *args, **kwargs): if not force_insert and self. Feedback! = self.
_old_feedback: self. Feedback_time = datetime.utcnow() return super(Package, self). Save(force_insert, force_update, *args, **kwargs).
1 Looks to me as though this will not do what you want in many cases. For example, on form submission, an object is initialized from POST parameters and then saved. The save method won't find a difference between the initial and current field values; but they ARE different from those in the database.
– Greg Ball Oct 5 '11 at 3:48.
Modifying the answer above... taking the brilliant function from Dominik Szopa and changing it will solve your relationship change detection: Use this: def get_changes_between_models(model1, model2, excludes = ): changes = {} for field in model1. _meta. Fields: if not (field.Name in excludes): if field.
Value_from_object(model1)! = field. Value_from_object(model2): changesfield.
Verbose_name = (field. Value_from_object(model1), field. Value_from_object(model2)) return changes Then in your code you can say (avoid try/except for performance reasons): if (self.
Id): old = MyModel.Objects. Get(pk=self.Id) changes = get_changes_between_models(self, old) if (changes): # Process based on what is changed. If you are doing this at the "model" level, there is no way to save the extra query.
The data has already been changed by the time you reach the "Save" point.My first post, so forgive me if I sound like an idiot.
Thanks for the tip on Field. Value_from_object()! Apparently it provides a consistent way of comparing values.
Nice! – Mandx Feb 19 '11 at 4:23.
In order to get differences of two model instances, you can also use this function. It compare to model instances and returns dictionary of changes.
It's useful! But seems that it isn't aware of related fields. For my purposes, adding or deleting a relationship (one-to-many or many-to-many) is also a change of the object.
– Mandx Mar 9 '10 at 16:41.
What you'll need to do is get an extra copy of the object you're working on from the database inside the save method before fully saving it. Example: class MyModel(models. Model): field1 = models.
CharField(max_length=50) def save(self): if self. Id: try: old = MyModel.objects. Get(pk=self.Id) if old.
Field1! = self. Field1: # Process somehow except MyModel.
DoesNotExist: pass super(MyModel, self).save().
I also came up with something like this, (retrieve the object from the DB before it's saved and compare it with the copy that is about to be saved) but I would like to save the extra query. – Mandx Mar 9 '10 at 16:36.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.