mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-20 02:06:42 -06:00
Refactor model_to_dict() to better handle tags
This commit is contained in:
parent
ab7b921641
commit
936e3424bb
@ -22,13 +22,14 @@ def model_to_dict(instance, fields=None, exclude=None):
|
||||
if key.startswith('_'):
|
||||
del model_dict[key]
|
||||
|
||||
# TODO: Differentiate between tags assigned to the instance and a M2M field for tags (ex: ConfigContext)
|
||||
elif key == 'tags':
|
||||
model_dict[key] = ','.join(sorted([tag.name for tag in model_dict['tags']]))
|
||||
|
||||
# Convert ManyToManyField to list of instance PKs
|
||||
elif model_dict[key] and type(model_dict[key]) in (list, tuple) and hasattr(model_dict[key][0], 'pk'):
|
||||
model_dict[key] = [obj.pk for obj in model_dict[key]]
|
||||
|
||||
if 'tags' in model_dict:
|
||||
model_dict['tags'] = ','.join(sorted([tag.name for tag in model_dict['tags']]))
|
||||
|
||||
return model_dict
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user