Komal Mestry
Komal Mestry

Reputation: 11

How to combine django datefield and timefield

I have tried this query:

start_date = DateField()
strt_time = TimeField()
obj=model.objects.annotate(
                datetimeobj=ExpressionWrapper(F('start_date') + F('strt_time'), output_field=DateTimeField()

It gives following error:

AttributeError: 'decimal.Decimal' object has no attribute 'tzinfo'

I have also tried this solution:

data=model.objects.annotate(datetime=datetime.combine('start_date','start_time'))

It gives error:

TypeError: combine() argument 1 must be datetime.date, not str

Thank you..

Upvotes: 1

Views: 942

Answers (1)

Dcoder14
Dcoder14

Reputation: 1939

The problem is in your code data=model.objects.annotate(datetime=datetime.combine('start_date','start_time')) as you are passing strings to the combine() method which expects date and time.

To merge date and time you are using datetime.combine() whose syntax is

datetime.combine(date, time)

So you have to do as:

data=model.objects.annotate(datetime=datetime.combine(start_date,start_time))

Upvotes: 2

Related Questions