Browse Source

Refactor model_to_dict() to better handle tags

Jeremy Stretch 6 years ago
parent
commit
936e3424bb
1 changed files with 4 additions and 3 deletions
  1. 4 3
      netbox/utilities/testing/utils.py

+ 4 - 3
netbox/utilities/testing/utils.py

@@ -22,13 +22,14 @@ def model_to_dict(instance, fields=None, exclude=None):
         if key.startswith('_'):
         if key.startswith('_'):
             del model_dict[key]
             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
         # 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'):
         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]]
             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
     return model_dict