When I load a record, either via get() or by looping through the
results of a .filter(), I'd like to keep track of the original
attribute values via setting a custom property on that model which
would contain a dict of the attribute/value at the time the record was
loaded ...
This is basically so that when I save, I can compare the current
values with those of the original to see which values have changed,
and which haven't, which I need to know for some event logs I'm
writing out at save time.
Thanks for any info.
You could hook yourself to pre_save:
http://www.djangoproject.com/documentation/db-api/#what-happens-when-you-save
kr, jure
On Aug 29, 1:21 pm, "Jure Čuhalev" <gandal...@gmail.com> wrote:
> http://www.djangoproject.com/documentation/db-api/#what-happens-when-...
>
> kr, jure
But by the time pre_save occurs, all the values have been changed, and
you would have to perform another get() before you would have all the
original values. I don't think that's quite as good.
Something you could do is override the __set__ (or do the same via a
descriptor), and from there you could easily populate your dict with
original values. Once there's a set value for each key, then don't
populate the dict any more. Off the cuff code:
d = {}
def __set__(self, obj, val):
if obj not in d:
d[obj] = val
super(SomeClass, self).__set__(obj, val)
I have no idea if that works, but it's one possible starting point. If
you do manage to get this working, post your solution, I think this one
might be a good starting point for those people who want to do the
"change-only" query saves to the DB too. (I know that there's a ticket
somewhere in Trac that is related to that topic, but I'm too lazy to
look it up).
Good luck,
gav
Something like this...
def backup_model_data(sender, instance, signal, *args, **kwargs):
instance._original_data = instance.__dict__.copy()
class YourModel(model.Models):
...
dispatcher.connect(backup_model_data,signal=signals.post_init,
sender=YourModel)
# some extra properties removed to make it shorter to read
from django.db.models import signals
from django.dispatch import dispatcher
from django.db import models
def backup_model_data(sender, instance, signal, *args, **kwargs):
instance._original_data = instance.__dict__.copy()
class MarketingStatus(models.Model):
marketing_status_id = models.AutoField(primary_key=True)
marketing_status_name = models.CharField(blank=True, maxlength=30)
marketing_status_description = models.CharField(blank=True,
maxlength=255)
def save(self):
testing =
self._original_data['marketing_status_name']
testing2 = self.marketing_status_name
triggererror = madeupvariabletotriggererror #
just stuck this here so I can view the data in the browser
super(MarketingStatus, self).save()
class Meta:
db_table = 'marketing_status'
dispatcher.connect(backup_model_data,signal=signals.post_init,sender=MarketingStatus)
See Malcolm's explanation here:
http://groups.google.com/group/django-users/msg/6d849eca95243371
-Mike
On Aug 30, 5:28 am, "carole.zie...@gmail.com"
In [2]: m=cm.MarketingStatus(marketing_status_name="Test Name",
marketing_status_description="Testing signals")
In [3]: m.save()
In [4]: m.marketing_status_name="Changed this"
In [5]: m.marketing_status_name
Out[5]: 'Changed this'
In [6]: m._original_data['marketing_status_name']
Out[6]: 'Test Name'
I added a couple print statements to print testing and testing 2 in
the save method:
In [2]: m=cm.MarketingStatus.objects.all()[0]
In [3]: m.marketing_status_name = "changed again"
In [4]: m.save()
testing: Test Name
testing2: changed again
----- using this code ----
def backup_model_data(sender, instance, signal, *args, **kwargs):
instance._original_data = instance.__dict__.copy()
class MarketingStatus(models.Model):
marketing_status_id = models.AutoField(primary_key=True)
marketing_status_name = models.CharField(blank=True, maxlength=30)
marketing_status_description =
models.CharField(blank=True,maxlength=255)
def save(self):
testing =self._original_data['marketing_status_name']
testing2 = self.marketing_status_name
print "testing: %s" % testing
print "testing2: %s" % testing2
super(MarketingStatus, self).save()
dispatcher.connect(backup_model_data,signal=signals.post_init,sender=MarketingStatus)
Thanks again everyone!
On Aug 29, 4:34 pm, "carole.zie...@gmail.com"