765π
it looks like datetime.now()
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)
or
date = models.DateTimeField(default=datetime.now, blank=True)
The difference between the second example and what you currently have is the lack of parentheses. By passing datetime.now
without the parentheses, you are passing the actual function, which will be called each time a record is added. If you pass it datetime.now()
, then you are just evaluating the function and passing it the return value.
More information is available at Djangoβs model field reference
198π
Instead of using datetime.now
you should be really using from django.utils.timezone import now
Reference:
- Documentation for
django.utils.timezone.now
so go for something like this:
from django.utils.timezone import now
created_date = models.DateTimeField(default=now, editable=False)
- [Django]-Changing a project name in django
- [Django]-How do Django models work?
- [Django]-How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?
34π
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(default=datetime.now,blank=True)
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-Django rest framework: query parameters in detail_route
- [Django]-How to loop over form field choices and display associated model instance fields
22π
David had the right answer. The parenthesis () makes it so that the callable timezone.now() is called every time the model is evaluated. If you remove the () from timezone.now() (or datetime.now(), if using the naive datetime object) to make it just this:
default=timezone.now
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 manage.py makemigrations/migrate.
I just encountered this. Much thanks to David.
- [Django]-What are the limitations of Django's ORM?
- [Django]-Data Mining in a Django/Postgres application
- [Django]-Django: Implementing a Form within a generic DetailView
13π
The datetime.now()
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.
- [Django]-Iterating over related objects in Django: loop over query set or use one-liner select_related (or prefetch_related)
- [Django]-How to get superuser details in Django?
- [Django]-Django substr / substring in templates
9π
datetime.now()
is being evaluated once, when your class is instantiated. Try removing the parenthesis so that the function datetime.now
is returned and THEN evaluated. I had the same issue with setting default values for my DateTimeField
s and wrote up my solution here.
- [Django]-Copy a database column into another in Django
- [Django]-Visual Editor for Django Templates?
- [Django]-How does one make logging color in Django/Google App Engine?
7π
From the Python language reference, under Function definitions:
Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same βpre-computedβ value is used for each call.
Fortunately, Django has a way to do what you want, if you use the auto_now
argument for the DateTimeField
:
date = models.DateTimeField(auto_now=True)
See the Django docs for DateTimeField.
- [Django]-Speeding up Django Testing
- [Django]-Testing nginx without domain name
- [Django]-Removing 'Sites' from Django admin page
7π
The answer to this one is actually wrong.
Auto filling in the value (auto_now/auto_now_add isnβt the same as default). The default value will actually be what the user sees if its a brand new object. What I typically do is:
date = models.DateTimeField(default=datetime.now, editable=False,)
Make sure, if your trying to represent this in an Admin page, that you list it as βread_onlyβ and reference the field name
read_only = 'date'
Again, I do this since my default value isnβt typically editable, and Admin pages ignore non-editables unless specified otherwise. There is certainly a difference however between setting a default value and implementing the auto_add which is key here. Test it out!
- [Django]-Django Template Language: Using a for loop with else
- [Django]-Disable session creation in Django
- [Django]-How do I get the object if it exists, or None if it does not exist in Django?
3π
In Django 3.0 auto_now_add
seems to work with auto_now
reg_date=models.DateField(auto_now=True,blank=True)
- [Django]-Django: how save bytes object to models.FileField?
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-How about having a SingletonModel in Django?
3π
if you need only DateField try this
date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
if you need Both Date and Time try this
date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
- [Django]-Error when using django.template
- [Django]-How do I do an OR filter in a Django query?
- [Django]-Duplicate column name