I recently ran into this issue when trying to store a form object in the session. Regardless of whether this is a good idea, if you find yourself staring at the error below and scratching your head, you might be interested in a quick and dirty workaround.
Traceback:
File "/Users/hs/code/venv_django_fail/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
188. response = middleware_method(request, response)
File "/Users/hs/code/venv_django_fail/lib/python2.7/site-packages/django/contrib/sessions/middleware.py" in process_response
36. request.session.save()
File "/Users/hs/code/venv_django_fail/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py" in save
52. session_data=self.encode(self._get_session(no_load=must_create)),
File "/Users/hs/code/venv_django_fail/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in encode
79. pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
Exception Type: PicklingError at /
Exception Value: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
Patching the Django code in my virtualenv seemed like a terrible idea. So instead I took the fixed code from the current Django development version and put it into a widgets.py file in my app directory. Let's say you have this form:
class FunnyForm(forms.Form):
name = forms.CharField(label=u"What's your name?")
is_awesome = forms.BooleanField(label=u'Are you awesome?', initial=True)
All you need to do is put this widgets.py file into your app directory, add
from widgets import PicklableCheckboxInputto the top of your views.py file and change the widget for the BooleanField in the form like this:
class FunnyForm(forms.Form):
name = forms.CharField(label=u"What's your name?")
is_awesome = forms.BooleanField(label=u'Are you awesome?', initial=True,
widget=PicklableCheckboxInput)