How do you do the same as this SQL in django?
UPDATE table SET timestamp=NOW() WHERE ...
Particularly I wish to set the datetime area using server's builtin function to obtain the system time in the server the database was running on and never time around the client machine.
I understand you are able to execute the raw sql directly but I am searching for a far more portable solution since databases have different functions to get the present datetime.
Edit: couple of people pointed out auto_now param. This updates the datetime on every modification while I wish to update datetime only on certain occasions.
As j0ker stated, if you would like automatic update from the timestamp, make use of the
auto_now option. E.g.
date_modified = models.DateTimeField(auto_now=True).
Or if you wish to get it done by hand, is not it an easy assignment with python
from datetime import datetime obj.date_modified = datetime.now()
If you would like the datetime from the foreign server (i.e., not the main one hosting the Django application), you are going to need to peg it by hand for any datatime to make use of. You could utilize a SQL command like
select now(); or something like that over SSH, like
ssh user@host "date +%s".
Perhaps you should have a look in to the documentation:
The choice 'auto_now' might be precisely what you are looking for. You may also utilize it using the DateTimeField. It updates the DateTime every time you are saving the model. So with this option looking for your DateTimeField it ought to be sufficent to retrieve an information-record and save it again to create time right.