putting a database object in a session

7 views
Skip to first unread message

Jeremy Jones

unread,
Mar 5, 2006, 12:00:54 PM3/5/06
to Django users
Is it bad form to put a database object in a session variable? It
really just feels wrong, but it works. Typically, I would just put the
object's ID in a session variable and retrieve the object when I next
needed it, but I accidentally forgot to do
``request.session["this_thing"] = obj.id`` and instead did
``request.session["this_thing"] = obj`` and I surprisingly found
myself with access to the object itself rather than just the ID of the
object. Anyway, I was wondering if it works by design and is thus
acceptable, or if it works by accident and should be avoided.

- jmj

Joseph Kocherhans

unread,
Mar 5, 2006, 4:06:56 PM3/5/06
to django...@googlegroups.com

http://www.djangoproject.com/documentation/sessions/#technical-details

You *can* do it, but I wouldn't recommend it. I can easily imagine a
situation where the version you have sotred in the session, and the
version stored in the database could get out of sync. Storing obj.id
and re-retreiving the object will probably be much safer. If you need
to avoid the extra database hit, use the caching framework. :)

Joseph

Reply all
Reply to author
Forward
0 new messages