django QueryDict only returns the last value of a list

The function below converts a QueryDict object to a python dictionary. It's a slight modification of Django's QueryDict.dict() method. But unlike that method, it keeps lists that have two or more items as lists.

def querydict_to_dict(query_dict):
    data = {}
    for key in query_dict.keys():
        v = query_dict.getlist(key)
        if len(v) == 1:
            v = v[0]
        data[key] = v
    return data

Usage:

data = querydict_to_dict(request.POST)

# Or

data = querydict_to_dict(request.GET)

From here

This is a feature, not a bug. If you want a list of values for a key, use the following:

values = request.POST.getlist('key')

And this should help retrieving list items from request.POST in django/python


You can use getlist method

data = request.POST.getlist('form_4606-parentspass_id','')

Tags:

Django