mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
model_to_dict(): Remove fields that start with an underscore
This commit is contained in:
@ -9,6 +9,7 @@ def model_to_dict(instance, fields=None, exclude=None):
|
|||||||
"""
|
"""
|
||||||
Customized wrapper for Django's built-in model_to_dict(). Does the following:
|
Customized wrapper for Django's built-in model_to_dict(). Does the following:
|
||||||
- Excludes the instance ID field
|
- Excludes the instance ID field
|
||||||
|
- Exclude any fields prepended with an underscore
|
||||||
- Convert any assigned tags to a comma-separated string
|
- Convert any assigned tags to a comma-separated string
|
||||||
"""
|
"""
|
||||||
_exclude = ['id']
|
_exclude = ['id']
|
||||||
@ -17,6 +18,10 @@ def model_to_dict(instance, fields=None, exclude=None):
|
|||||||
|
|
||||||
model_dict = _model_to_dict(instance, fields=fields, exclude=_exclude)
|
model_dict = _model_to_dict(instance, fields=fields, exclude=_exclude)
|
||||||
|
|
||||||
|
for key in list(model_dict.keys()):
|
||||||
|
if key.startswith('_'):
|
||||||
|
del model_dict[key]
|
||||||
|
|
||||||
if 'tags' in model_dict:
|
if 'tags' in model_dict:
|
||||||
model_dict['tags'] = ','.join(sorted([tag.name for tag in model_dict['tags']]))
|
model_dict['tags'] = ','.join(sorted([tag.name for tag in model_dict['tags']]))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user