开发者

Trying to combine lists with DateTimeField and date object in Django

I've got 2 Django query lists with a Title and a Created_At DateTimeField (Articles and Projects), I have another list which is the result of a python function that returns a string Title and a Create_At date object.

I want to chain these lists together so I can combine them and sort the results by their Date fields - however at the moment I'm getting the following error:

can't compare datetime.datetime to tuple

How do I get these two lists together? The code causing the error is:

latest_changes = sorted(
    chain(articles, projects, tweets),
    key=lambda instance: instance.created)

OR Is there a better way to do this instead of using chain? Ideally I'd like to create a new list without having to have the individual list field names matching as well开发者_开发问答 (I gather chain matches like to like on names).


I think the tweets.created is a tuple rather than a Datetime object. You can use the dateutil module to convert the tuple into a proper Datetime object in python and then you can compare seamlessly :)


Judging by the error message, apparently instance.created is a tuple for one of your chained iterables.

My guess is that it's whichever item is the "result of a python function that returns a string Title and Create_At date object" that you were talking about, as a DateTimeField wouldn't return a tuple.

Does type(tweets[0].created) == tuple return True?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜