Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: 'tuple' object has no attribute 'strftime'

I created a list of values of datetime objects

a=design_list.values_list('date_submitted')

So I get:

[(datetime.datetime(2012, 10, 21, 13, 56, 24),), (datetime.datetime(2012, 10, 21, 10, 33, 58),), etc...

I'm trying to convert them to timestamps with, say:

timestamps[0] = a[0].strftime("%s")

But I get the error:

AttributeError: 'tuple' object has no attribute 'strftime'

Can anyone tell me what I'm doing wrong?

like image 315
babbaggeii Avatar asked Apr 10 '26 13:04

babbaggeii


2 Answers

I added flat=True to the query:

a=design_list.values_list('date_submitted', flat=True)

Which gives:

[datetime.datetime(2012, 10, 21, 13, 56, 24), datetime.datetime(2012, 10, 21, 10, 33, 58), 

Which then can be converted.

like image 156
babbaggeii Avatar answered Apr 12 '26 08:04

babbaggeii


You're referring to the tuple instead of the first element of the tuple. Try a[0][0].strftime('%s').

>>> a = [(datetime.datetime(2012, 10, 21, 13, 56, 24),)]
>>> a[0][0].strftime('%s')
'1350852984'

This is the reason for why you got the error, but as you correctly pointed out, flattening the resulting list is the right approach here.

like image 40
RocketDonkey Avatar answered Apr 12 '26 10:04

RocketDonkey