Django datetime issues (


I have the below db model:

from datetime import datetime    

class TermPayment(models.Model):
    # I have excluded fields that are irrelevant to the question
    date = models.DateTimeField(, blank=True)

I add a new instance by using the below:

tp = TermPayment.objects.create(**kwargs)

My issue: all records in database have the same value in date field, which is the date of the first payment. After the server restarts, one record has the new date and the other records have the same as the first. It looks as if some data is cached, but I can't find where.

database: mysql 5.1.25

django v1.1.1

5/13/2019 12:55:48 PM

Accepted Answer

it looks like is being evaluated when the model is defined, and not each time you add a record.

Django has a feature to accomplish what you are trying to do already:

date = models.DateTimeField(auto_now_add=True, blank=True)


date = models.DateTimeField(, blank=True)

The difference between the second example and what you currently have is the lack of parentheses. By passing without the parentheses, you are passing the actual function, which will be called each time a record is added. If you pass it, then you are just evaluating the function and passing it the return value.

More information is available at Django's model field reference

5/5/2010 9:01:19 AM

Instead of using you should be really using from django.utils.timezone import now


so go for something like this:

from django.utils.timezone import now

created_date = models.DateTimeField(default=now, editable=False)

From the documentation on the django model default field:

The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.

Therefore following should work:

date = models.DateTimeField(,blank=True)

David had the right answer. The parenthesis () makes it so that the callable is called every time the model is evaluated. If you remove the () from (or, if using the naive datetime object) to make it just this:

Then it will work as you expect:
New objects will receive the current date when they are created, but the date won't be overridden every time you do makemigrations/migrate.

I just encountered this. Much thanks to David.


The is evaluated when the class is created, not when new record is being added to the database.

To achieve what you want define this field as:

date = models.DateTimeField(auto_now_add=True)

This way the date field will be set to current date for each new record.

2010/05/05 is being evaluated once, when your class is instantiated. Try removing the parenthesis so that the function is returned and THEN evaluated. I had the same issue with setting default values for my DateTimeFields and wrote up my solution here.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]