Making labels additive and not adding a many item to config if already in parent

This commit is contained in:
John Westcott IV
2022-09-09 12:11:14 -04:00
committed by Alan Rominger
parent 68e11d2b81
commit e076f1ee2a
3 changed files with 13 additions and 7 deletions

View File

@@ -531,13 +531,16 @@ def copy_m2m_relationships(obj1, obj2, fields, kwargs=None):
src_field_value = getattr(obj1, field_name)
if kwargs and field_name in kwargs:
override_field_val = kwargs[field_name]
# TODO: Should we spike this our or just put the for loop inside the next if and make everything respect order?
if field_name == 'instance_groups':
# instance_groups are a list but we need to preserve the order
for ig_id in override_field_val:
getattr(obj2, field_name).add(ig_id)
continue
if isinstance(override_field_val, (set, list, QuerySet)):
# Labels are additive so we are going to add any src labels in addition to the override labels
if field_name == 'labels':
for jt_label in src_field_value.all():
getattr(obj2, field_name).add(jt_label.id)
getattr(obj2, field_name).add(*override_field_val)
continue
if override_field_val.__class__.__name__ == 'ManyRelatedManager':