Question

newb to python and pyramid/pylons. After a user logs in, I want to grab some data in my database and store it in a session variable. I've been able to store the user's login id with remember(). Using request.session, I can set a session variable as long as it's a string but if I try to retrieve something from my database, I get an error: "BadPickleGet: �". Here's the relevant part of my view:

if 'form.submitted' in request.params:
        login = request.params['login']
        password = request.params['password']
        if User.check_password(login, password):
            headers = remember(request, login) # ie dave
            session = request.session
            #session['myvar'] = 'somethinghardcoded' # this works!
            session['myvar'] = User.myfield # this doesn't work!
            session.save()
            return HTTPFound(location=came_from, headers=headers)
Was it helpful?

Solution

In your example you are not dealing with an instance of User that you load from the database. This would be something like user = DBSession.query(User).filter_by(login).first(). There is a big difference between User.myfield and user.myfield, where the first is just the Column object or property and the second is the actual data for a specific row in the database. Your error is probably just in the inability to pickle a Column object. The session can pickle primitive types (ints, strings, etc) just fine.

OTHER TIPS

Pyramid Sessions:

Keys and values of session data must be pickleable. This means, typically, that they are instances of basic types of objects, such as strings, lists, dictionaries, tuples, integers, etc. If you place an object in a session data key or value that is not pickleable, an error will be raised when the session is serialized.

This means: Your data can not be pickled and you need to provide a method to return it in a format that can be pickled (I like to make a method called todict()).

But there seems to be a second problem:

[...]in my database?

The default session factory (which I assume you are using), does not store your data in a database but in a cookie (where it is unencrypted). You could take a look at pyramid_beaker, that may be the right thing for you here. In that case you still need data that can be pickled (a dict, list, string, ...) I assume, but then its in the database and not in a cookie so you can store confidential information in it (don't do that in the default!) and remove limitation of length (4kB for cookies).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top