MCPcopy
hub / github.com/encode/django-rest-framework / set_value

Method set_value

rest_framework/serializers.py:357–375  ·  view source on GitHub ↗

Similar to Python's built in `dictionary[key] = value`, but takes a list of nested keys instead of a single key. set_value({'a': 1}, [], {'b': 2}) -> {'a': 1, 'b': 2} set_value({'a': 1}, ['x'], 2) -> {'a': 1, 'x': 2} set_value({'a': 1}, ['x', 'y'], 2) -> {'a

(self, dictionary, keys, value)

Source from the content-addressed store, hash-verified

355 }
356
357 def set_value(self, dictionary, keys, value):
358 """
359 Similar to Python's built in `dictionary[key] = value`,
360 but takes a list of nested keys instead of a single key.
361
362 set_value({'a': 1}, [], {'b': 2}) -> {'a': 1, 'b': 2}
363 set_value({'a': 1}, ['x'], 2) -> {'a': 1, 'x': 2}
364 set_value({'a': 1}, ['x', 'y'], 2) -> {'a': 1, 'x': {'y': 2}}
365 """
366 if not keys:
367 dictionary.update(value)
368 return
369
370 for key in keys[:-1]:
371 if key not in dictionary:
372 dictionary[key] = {}
373 dictionary = dictionary[key]
374
375 dictionary[keys[-1]] = value
376
377 @cached_property
378 def fields(self):

Callers 4

to_internal_valueMethod · 0.95
test_no_keysMethod · 0.80
test_one_keyMethod · 0.80
test_nested_keyMethod · 0.80

Calls 1

updateMethod · 0.45

Tested by 3

test_no_keysMethod · 0.64
test_one_keyMethod · 0.64
test_nested_keyMethod · 0.64