I have a model name CustomerProject and I want to track individually few fields when it was last updated. Right now I am using auto_now= True in datetime fields which can tell me when the whole model was last edited but I want to track individually few boolean fields such as when the work started? when the work delivered etc. here is my
An alternative would be to use one of the several packages available that let you set up audit/change history for django tables. Then you could check when the last change was made and compare fields to see exactly what had changed.
Depends on your use case as to whether this is a sensible solution for you or not.
KenWhitesell currently I am using DateTimeField in my models and it’s telling me when the whole model was last updated but I want to keep track my each model fields assume I have two fields in my model and I edited only first fields and I want to know when the first fields was last updated. does it make sense??
However, there’s no built-in facility for doing that. You’re going to need to do that manually (or, as @michjnich points out, find a third-party package that does it for you - which effectively turns out to be the same thing.)
Signals are almost never the right answer, unless you encounter a situation where there is no other way of doing something. They don’t act the way a lot of people think they act, and they have a number of limitations that can cause problems if you’re not aware of them. I’ve never found them to be worth the hassle and problems.
“Manually” in this case means that anytime someone submits the form with those fields on them, you need to check to see if they had been changed. If they have, then you need to update your fields that track the most recent changes with the current timestamp.
@MDFARHYN Can I ask the reason for needing to track each individual field change? It’s an unusual requirement, and I suspect you may be trying to do something that could better be solved by an alternative approach.
To do this manually, you could change the save method for the model to check whether each field had changed, and then update its related timestamp. This adds extra overhead and potential ommissions when making model changes in the future.
It could also be done, as @KenWhitesell says, when submitting the form, which may logically make more sense, but if there are many points in your system that can change the model values, it would require duplicating the logic at all points.
Understanding your underlying requirement and the reason for it may help a little here.