I had a similar problem when importing data from another application using the import data script executed from a shell command. Since it is dynamically impossible to disable the auto_now_add or auto_now function, I had to use a modified settings module.
First we define the IMPORT variable in the source settings module:
IMPORT = False
and created a new module import_settings containing only:
from my_app.settings import * IMPORT = True
Then, defining my models, I added the code
from django.conf import settings if settings.IMPORT: AUTO_NOW = False else: AUTO_NOW = True
and used the value AUTO_NOW when setting DateTimeField
created = models.DateTimeField(editable=False, null=True, blank=True, auto_now_add=AUTO_NOW) modified = models.DateTimeField(editable=False, null=True, blank=True, auto_now=AUTO_NOW)
Finally, I started importing the script data using the -settings option of the control command:
python manage.py shell --settings=my_app.import_settings
gtoffoli Jan 02 '17 at 12:23 2017-01-02 12:23
source share