Reputation: 1697
Trying to combine date & time and received error:
descriptor 'date' requires a 'datetime.datetime' object but received a 'datetime.date'
from datetime import date, datetime, time, timedelta
instance = Model.objects.get(pk=id)
datetime = datetime.combine(datetime.date(instance.mydate), datetime.time(instance.mytime))
Edit:
mydate and mytime are stored here:
class Model(models.Model):
mydate = models.DateField(blank=True, null=True)
mytime = models.TimeField(blank=True, null=True)
Edit 2:
I did have this in place but was looking for a 1 line solution:
mydate = datetime.strptime(str(instance.mydate), '%Y-%m-%d')
mytime = datetime.strptime(str(instance.mytime), '%H:%M:%S')
datetime = datetime.combine(datetime.date(mydate), datetime.time(mytime))
Upvotes: 0
Views: 8903
Reputation: 90879
The issue is that you are actually trying to call datetime.datetime.date()
, you should be calling datetime.date()
. I am guessing you need to use datetime.time()
instead of datetime.datetime.time()
also.
Also, according to documentation , the parameters to datetime.date()
are three integers indicating the year, month and day of the date.
You should do -
datetime = datetime.combine(date(instance.mydate.year, instance.mydate.month, instance.mydate.day), time(instance.mytime.hour, instance.mytime.minute, instance.mytime.second))
Give the time, upto the accuracy you want, As an example I gave till second
, documentation for that is here.
Upvotes: 4