On Thu, Oct 10, 2013 at 2:55 PM, DJ-Tom <
event...@gmail.com> wrote:
>
> Thanks, but *how* can I create a hash from a database object in Django - is
> there a generic method to iterate over all (or only specific) fields and get
> a hash value?
>
This is not a full solution, but you can select out extra fields (and
then subsequently filter on them) using the .extra() method on
querysets.
Eg, if you were using mysql and wanted a field that was a hash of pk
(id), first_name and last_name fields, then you could do something
like so:
>>> User.objects.filter(id__lt=10).extra(
... select={'hash': 'MD5(CONCAT(id, first_name,
last_name))'}).values_list('hash')
[('a2147bd4161f55a7ca402d8971b6a45e',),
('663dfc6f51e0ef2c9fa6691527722f20',),
('045dde540781fe013c58c3ebb9383df6',),
('70079b2943da078f437a3678578c78f7',),
('2b997848117758e1844875b1119702bb',),
('2602ecfa423f969614af5b6174c6863e',),
('c9f0f895fb98ab9159f51fd0297e236d',)]
I don't know that this necessarily helps in your aim though. It might
be better to have a custom save method which updates a
"report_fields_modified" timestamp column when fields that affect your
report are modified.
Unfortunately, (IIRC) you cannot easily determine which fields have
been modified without first loading a pristine copy of the object you
are saving from the database, which has obvious performance issues.
Cheers
Tom