Search code examples
pythondjangodjango-modelsdjango-formsdjango-auditlog

What happens after Django clean method but before commit?


I am using an awesome library called Django Auditlog. It tracks what changes occurred to an object. For model Book if I changed the Author name from 'John' to 'Mary' it records the before value (John), after value (Mary), when it occurred, and what user made the change.

It's working except that it is detecting changes that aren't changes for some of my decimal fields. It thinks a change occurs anytime I save this model: it thinks that I started with .00 and changed it to .0. But .00 is the existing value and I didn't change anything. I just saved() the record.

enter image description here

I checked the output of the form itself in the clean() method:

services_sub_total
44.00
sum_payments
33.00

And then in the database:

enter image description here

I just can't figure out where/why this is detecting only one zero in the decimal place - I'm not seeing that .0 anywhere. So I'm wondering what is happening between the clean() method and the commit that might be truncating the .00 to .0 where Auditlog might be incorrectly detecting a change?


Solution

  • If you look up into source you would see that it compares these two values not as field values but through use of smart_text values are converted to string inside get_field_value

    So it basically compares "44.00" and your value of "44.0" and this is different string so it is triggered as change

    Not sure if this is by design as in my eyes would be considered bug, and you could fork and fix it yourself