Django – Bug in QueryDict and Storing them in Sessions
Posted: February 22, 2009 Filed under: Django Leave a comment »Hello All,
Django, Django … I came across a bug in Django when I was storing request.POST in session variable. Why would I want to do this. Well, I am creating a survey component and I want to have the ability for the users to go to previous pages of the survey, and see or change what their answers are. So I do something like this on each page post:
request.session[str(category_id)] = request.POST
I have each category on a separate page. So when I go back or forward on each survey page I retrieve the session corresponding to the category and restore the forms:
form_list = create_category_forms(category_id, request.session[str(category_id)])
I have some forms on the pages that I have created that contain MultipleChoiceField and Choicefield types in the forms. The problem is related to the MultipleChoiceField, because these contain widgets such as: CheckboxSelectMultiple, which contain a list of values when submitted. Now when the post is submitted request.POST is essentially a QueryDict type. When this is stored in a session variable then retrieved on the next post back only the last element of the CheckSelectMultiple is kept. For example:
I select 1, 2, 3, 4 from the MultipleChoiceField, save it the request.POST in the session then retrieve it later I only get the value 4, not 1, 2, 3, 4. This is due to a bug on the pickling(serialization and reversing the serialization) of QueryDict.
The patch can be found here:http://code.djangoproject.com/ticket/10184 I am told when addressing this issue further with Django contributors and making it sure it works, that it will be corrected in the upcoming release 1.1 of Django. So for now get the patch because QueryDict has a bug with multiple values in session variables.
TTYL,
Fritz