I have a simple view
def foo(request):
card = Card.objects.latest(datetime)
request.session[\'card\']=card
For the above code I get the
Unfortunately the suggested answer does not work if the object is not a database object but some other kind of object - say, datetime or an object class Foo(object): pass that isn't a database model object.
Sure, if the object happen to have some id field you can store the id field in the database and look up the value from there but in general it may not have such a simple value and the only way is to convert the data to string in such a way that you can read that string and reconstruct the object based on the information in the string.
In the case of a datetime object this is made more complicated by the fact that while a naive datetime object can print out format %Z by simply not printing anything, the strptime object cannot read format %Z if there is nothing, it will choke unless there is a valid timezone specification there - so if you have a datetime object that may or may not contain a tzinfo field you really have to do strptime twice once with %Z and then if it chokes without the %Z. This is silly. It is made even sillier by the fact that datetime objects have a fromtimestamp function but no totimestamp function that uniformly produces a timestamp that fromtimestamp will read. If there is a format code that produces timestamp number I haven't found one and again, strftime/strptime suffer from the fact that they are not symmetric as described above.