Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin datefield-datetimefield confusion

I have a model similar to given below, in django 1.1.2:

from datetime import datetime
...
class Blah(models.Model):
    ...
    date = models.DateField(default=datetime.today)

in admin interface, default value for date is given as datetime (yyyy-mm-dd hh:mm:ss.ms) on each new record. it complaints that the value does not match the field constaints if i forgot to correct the value by removing the hour part.

how can i fix it?

like image 263
altunyurt Avatar asked Jan 21 '23 15:01

altunyurt


1 Answers

If you want a date, use datetime.date.today instead of datetime.datetime.today. Always import the datetime module, not the class, so you can tell which is which.

import datetime
...
class Blah(models.Model):
    ...
    date = models.DateField(default=datetime.date.today)
like image 86
Daniel Roseman Avatar answered Jan 30 '23 19:01

Daniel Roseman