Django 中是否存在 how data_get (Laravel) 的辅助函数?
Do it exist a helper function how data_get (Laravel) in Django?
data_get 函数使用“点”表示法从嵌套数组或对象中检索值:
$data = ['products' => ['desk' => ['price' => 100]]];
$price = data_get($data, 'products.desk.price');
// 100
中有更多详细信息
我做这个功能:
def get_data(data, dot_path, default=None):
arr_paths = dot_path.split('.')
result = data
for path in arr_paths:
try:
if isinstance(result, (dict, list, tuple)):
result = result[path]
else:
result = None
except KeyError as e:
result = None
if not result:
result = default
return result
data_get 函数使用“点”表示法从嵌套数组或对象中检索值:
$data = ['products' => ['desk' => ['price' => 100]]];
$price = data_get($data, 'products.desk.price');
// 100
中有更多详细信息
我做这个功能:
def get_data(data, dot_path, default=None):
arr_paths = dot_path.split('.')
result = data
for path in arr_paths:
try:
if isinstance(result, (dict, list, tuple)):
result = result[path]
else:
result = None
except KeyError as e:
result = None
if not result:
result = default
return result