Object 'datetime.date' does not have attribute 'date'

This code:

import datetime d_tomorrow = datetime.date.today() + datetime.timedelta(days=1) class Model(models.Model): ... timeout = models.DateTimeField(null=True, blank=True, default=d_tomorrow) ... 

returns this error:

 'datetime.date' object has no attribute 'date' 

What am I doing wrong?

+7
django datetime django-models
source share
5 answers

Problem resolved:

 from datetime import datetime, time, date, timedelta def tomorrow(): d = date.today() + timedelta(days=1) t = time(0, 0) return datetime.combine(d, t) 

Update 2015:

Arrow makes this a lot more direct.

Arrow is a Python library that offers a reasonable, human-friendly approach to creating, manipulating, formatting, and converting dates, times, and timestamps. It implements and updates the datetime type, blocks gaps in functionality and provides an intelligent API module that supports many common creation scenarios. Simply put, this helps you work with dates and times with less import and much less code.

The arrow is strongly inspired by moments and requests.

+2
source share

d_tomorrow is expected that ORM Django has a date attribute (apparently), but does not.

Anyway, you probably want to use the date called for the default date; otherwise, each model date will default to tomorrow relative to the time the model class was initialized, rather than the time the model was created. You can try the following:

 import datetime def tomorrow(): return datetime.date.today() + datetime.timedelta(days=1) class Model(models.Model): timeout = models.DateTimeField(null=True, blank=True, default=tomorrow) 
+13
source share

I had this problem when using the model from django.contrib.admin. I had two similar models, both with a date field (and both using auto_now_date = True - a complete red herring); one worked, one had this error.

Turned off

 def __unicode__(self): return self.date 

goes bang and this one

 def __unicode__(self): return u'%s' % self.date 

works just fine. What is obvious after the event, as usual.

+2
source share

This works for me:

 import datetime from datetime import timedelta tomorrow = datetime.date.today() + timedelta(days=1) class Test(models.Model): timeout = models.DateTimeField(db_index=True, default=tomorrow) 

Alternatively, you can use tomorrow = datetime.datetime.now() + timedelta(days=1)

0
source share

I tried my code and it worked perfectly. Can you verify that you are somehow not modifying / overriding the import?

Also try the following:

 import datetime as DT d_tomorrow = DT.date.today() + DT.timedelta(days=1) class Model(models.Model): timeout = models.DateTimeField(null=True, blank=True, default=d_tomorrow) 
-one
source share

All Articles