I have a simple view
def foo(request):
card = Card.objects.latest(datetime)
request.session[\'card\']=card
For the above code I get the
In a session, I'd just store the object primary key:
request.session['card'] = card.id
and when loading the card from the session, obtain the card again with:
try:
card = Card.objects.get(id=request.session['card'])
except (KeyError, Card.DoesNotExist):
card = None
which will set card
to None
if there isn't a card
entry in the session or the specific card doesn't exist.
By default, session data is serialised to JSON. You could also provide your own serializer, which knows how to store the card.id
value or some other representation and, on deserialization, produce your Card
instance again.