mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-19 03:42:25 -06:00
Enable dictionary specification of related objects in API
This commit is contained in:
@@ -85,6 +85,38 @@ def serialize_object(obj, extra=None):
|
||||
return data
|
||||
|
||||
|
||||
def dict_to_filter_params(d, prefix=''):
|
||||
"""
|
||||
Translate a dictionary of attributes to a nested set of parameters suitable for QuerySet filtering. For example:
|
||||
|
||||
{
|
||||
"name": "Foo",
|
||||
"rack": {
|
||||
"facility_id": "R101"
|
||||
}
|
||||
}
|
||||
|
||||
Becomes:
|
||||
|
||||
{
|
||||
"name": "Foo",
|
||||
"rack__facility_id": "R101"
|
||||
}
|
||||
|
||||
And can be employed as filter parameters:
|
||||
|
||||
Device.objects.filter(**dict_to_filter(attrs_dict))
|
||||
"""
|
||||
params = {}
|
||||
for key, val in d.items():
|
||||
k = prefix + key
|
||||
if isinstance(val, dict):
|
||||
params.update(dict_to_filter_params(val, k + '__'))
|
||||
else:
|
||||
params[k] = val
|
||||
return params
|
||||
|
||||
|
||||
def deepmerge(original, new):
|
||||
"""
|
||||
Deep merge two dictionaries (new into original) and return a new dict
|
||||
|
||||
Reference in New Issue
Block a user