Storing models in sessions

16 views
Skip to first unread message

Maniac

unread,
Aug 29, 2005, 8:15:36 PM8/29/05
to django...@googlegroups.com
Hi!

I have a model in my app representing some kind of client's working
environment. For registered clients I refer this environment model from
a client's model. And for non-registered users of site I'm going to
create temporary environments. Naturally I want to use Django's sessions
for these temporary instances.

One way of doing it is to store entire model object in a session (in
fact I don't know if it would work: is Django's models pickleable?) But
this seems strange: to store what is effectively a table row in some
serialized representation as a huge string. Or is it Ok?

Another way is to store in session only an ID of an object. But then I
need to hook on session expiration to delete the temporary model object.
Is it possible?

Chris Ryland

unread,
Aug 31, 2005, 1:30:00 AM8/31/05
to Django users
I'm still learning Django, but it seems like you'd have an easier time
if you went with the general flow and simply used Django's ORM for
everything, rather than trying to do something special for this case.

Or am I misunderstanding your situation?

Maniac

unread,
Aug 31, 2005, 10:28:50 AM8/31/05
to django...@googlegroups.com
Chris Ryland wrote:

>I'm still learning Django, but it seems like you'd have an easier time
>if you went with the general flow and simply used Django's ORM for
>everything, rather than trying to do something special for this case.
>
>
This is what I'm trying to do.

I use Django's model objects to store a users environment. It's not just
some options like prefered color or something instead it's a full blown
model. To be specific it's an application for ordering printing of
digital pictures. The users environment consists of a desktop with image
thumbnails, thumbnails know their position on the desktop, and also
store all the printing parameters like paper type, size, special framing
etc.

Speaking generally I wonder how (or wether) Django's sessions are
suitable to store such rather large structured data.

As the last resort I can implement my one sessions but I don't want to
invent this bicycle one more time :-)

Jason F. McBrayer

unread,
Aug 31, 2005, 1:38:14 PM8/31/05
to django...@googlegroups.com
On Wed, 2005-08-31 at 14:28 +0400, Maniac wrote:

> I use Django's model objects to store a users environment. It's not just
> some options like prefered color or something instead it's a full blown
> model. To be specific it's an application for ordering printing of
> digital pictures. The users environment consists of a desktop with image
> thumbnails, thumbnails know their position on the desktop, and also
> store all the printing parameters like paper type, size, special framing
> etc.
>
> Speaking generally I wonder how (or wether) Django's sessions are
> suitable to store such rather large structured data.

I think you would be better off storing the model objects as usual, but
adding a couple of flags to your model: one to indicate that this
object is a temporary/unregistered user, and one to store when the
object should be expired. You can then either periodically do batch
expiry, or some other system. I assume that once no sessions refer to
one of those users anymore, having an inactive temporary user model
hanging around doesn't cost you anything except database space which you
can reclaim when it's convenient.

--
+----------------------------------------------------------------+
| Jason F. McBrayer jmc...@carcosa.net |
| "If you wish to make Pythocles wealthy, don't give him more |
| money; rather, reduce his desires." -- Epicurus |


Reply all
Reply to author
Forward
0 new messages