Jure Čuhalev wrote:
> On 8/29/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>> Since there isn't a load() method on a model... I'm thinking I somehow
>> have to do this on the manager..but not sure ... any tips would be
>> great.
>>
>> 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
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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---