[Answer]-How to save 'arrays' to variable from QueryDict

1đź‘Ť

âś…

Django’s QueryDict has a few additional methods to deal with multiple values per key compared to the traditional dict; useful for your purposes are:

QueryDict.iterlists() Like QueryDict.iteritems() except it includes
all values, as a list, for each member of the dictionary.

QueryDict.getlist(key, default)
Returns the data with the requested key, as a Python list. Returns an empty list if the key doesn’t exist and no default value was provided. It’s guaranteed to return a list of some sort unless the default value was no list.

QueryDict.lists()
Like items(), except it includes all values, as a list, for each member of the dictionary.

So you can do something like:

qd = QueryDict(...)
for values in qd.lists():
    for value in values:
        do_something(value)

Also note that the “normal” dict methods like get always return only a single value (the last value for that key).

Leave a comment