A subclass of dictionary customized to handle multiple values for the same key.
Constructor
Signature
def MultiValueDict(
key_to_list_mapping: tuple|dict = ()
)
Parameters
| Name | Type | Description |
|---|
| key_to_list_mapping | `tuple | dict` = () |
Methods
get()
@classmethod
def get(
key: any,
default: any = None
) - > any
Return the last data value for the passed key. If key doesn't exist or value is an empty list, return default.
Parameters
| Name | Type | Description |
|---|
| key | any | The key to retrieve. |
| default | any = None | The value to return if the key is not found or has no values. |
Returns
| Type | Description |
|---|
any | The last value in the list for the key, or the default value if the key is missing or empty. |
getlist()
@classmethod
def getlist(
key: any,
default: any = None
) - > list
Return the list of values for the key. If key doesn't exist, return a default value.
Parameters
| Name | Type | Description |
|---|
| key | any | The key to look up. |
| default | any = None | The value to return if the key is not found. |
Returns
| Type | Description |
|---|
list | A copy of the list of values for the specified key. |
setlist()
@classmethod
def setlist(
key: any,
list_: list
) - > null
Sets the entire list of values for a specific key.
Parameters
| Name | Type | Description |
|---|
| key | any | The key to update. |
| list_ | list | The list of values to associate with the key. |
Returns
setdefault()
@classmethod
def setdefault(
key: any,
default: any = None
) - > any
Returns the value of the key if it exists; otherwise, sets the key to the default value and returns it.
Parameters
| Name | Type | Description |
|---|
| key | any | The key to check or set. |
| default | any = None | The value to set if the key is missing. |
Returns
| Type | Description |
|---|
any | The value associated with the key after the operation. |
setlistdefault()
@classmethod
def setlistdefault(
key: any,
default_list: list = None
) - > list
Returns the list of values for a key; if the key is missing, it initializes the key with the provided default list.
Parameters
| Name | Type | Description |
|---|
| key | any | The key to check or set. |
| default_list | list = None | The list to use as a default if the key is missing. |
Returns
| Type | Description |
|---|
list | The list of values associated with the key. |
appendlist()
@classmethod
def appendlist(
key: any,
value: any
) - > null
Append an item to the internal list associated with key.
Parameters
| Name | Type | Description |
|---|
| key | any | The key whose list should be appended to. |
| value | any | The value to add to the end of the key's list. |
Returns
items()
@classmethod
def items() - > generator
Yield (key, value) pairs, where value is the last item in the list associated with the key.
Returns
| Type | Description |
|---|
generator | A generator yielding tuples of keys and their respective last values. |
lists()
@classmethod
def lists() - > iterator
Yield (key, list) pairs.
Returns
| Type | Description |
|---|
iterator | An iterator yielding tuples of keys and their full list of values. |
values()
@classmethod
def values() - > generator
Yield the last value on every key list.
Returns
| Type | Description |
|---|
generator | A generator yielding the final element from each key's value list. |
copy()
@classmethod
def copy() - > [MultiValueDict](multivaluedict.md?sid=django_utils_datastructures_multivaluedict)
Return a shallow copy of this object.
Returns
| Type | Description |
|---|
[MultiValueDict](multivaluedict.md?sid=django_utils_datastructures_multivaluedict) | A new instance of MultiValueDict containing the same data. |
update()
@classmethod
def update(
args: any,
kwargs: any
) - > null
Extend rather than replace existing key lists.
Parameters
| Name | Type | Description |
|---|
| args | any | A mapping or iterable of tuples containing keys and values to add. |
| kwargs | any | Key-value pairs to add to the dictionary. |
Returns
dict()
@classmethod
def dict() - > dict
Return current object as a dict with singular values.
Returns
| Type | Description |
|---|
dict | A standard dictionary where each key maps only to its last value. |