babbaggeii
babbaggeii

Reputation: 7737

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?

Upvotes: 2

Views: 6853

Answers (2)

RocketDonkey
RocketDonkey

Reputation: 37259

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.

Upvotes: 5

babbaggeii
babbaggeii

Reputation: 7737

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.

Upvotes: 5

Related Questions