Django how to track each models fields when it was last updated? or is it possible to use autonow in boolean or char fields?

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

models.py

class CustomerProject(models.Model):
      project_title = models.CharField(max_length=2000,blank=True,null=True)
      project_updated_at = models.DateTimeField(auto_now= True,blank=True,null=True)
      #I want to track separately those  BooleanField 
      project_started = models.BooleanField(default=True)
      wting_for_sample_work = models.BooleanField(default=False)
      sample_work_done = models.BooleanField(default=False)
      working_on_final_project = models.BooleanField(default=False)
      project_deliverd = models.BooleanField(default=False)
      project_closed = models.BooleanField(default=False)

There is no “automatic” way of managing this. You’ll have to do it manually by tracking when a field has been changed and saving the timestamp when that change was submitted.

1 Like

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.

1 Like

KenWhitesell can you please little bit tell about timestamp? How to apply theme in fields ?

In general terms, a timestamp is just another name for a DateTimeField used to mark when something happened. There’s nothing special or unusual about it.

1 Like

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??

Yes, I understand what you’re trying to do.

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.)

what do you mean by manually ? I am thinking using signals for that.

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.

1 Like

@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.