From 357d6d0852b47ce2d5d571511f6ffa34ef76d22b Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Tue, 3 Oct 2023 16:17:09 +0200 Subject: [PATCH] fix(apis_entities): only set collection if the attribute exists `collection` is a leftover of the TempEntityClass. some projects still use that, so we set it if it exists, but we check first if the object we are working with even has such an attribute. Closes: #313 --- apis_core/apis_entities/forms.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/apis_core/apis_entities/forms.py b/apis_core/apis_entities/forms.py index 6ce9cae3c..9d81dfa80 100644 --- a/apis_core/apis_entities/forms.py +++ b/apis_core/apis_entities/forms.py @@ -225,12 +225,18 @@ def sort_fields_list(field_names, entity_name): def save(self, *args, **kwargs): obj = super(GenericEntitiesForm, self).save(*args, **kwargs) - if obj.collection.all().count() == 0: - col_name = getattr( - settings, "APIS_DEFAULT_COLLECTION", "manually created entity" - ) - col, created = Collection.objects.get_or_create(name=col_name) - obj.collection.add(col) + + # backwards compatibility + # collection is a field of TempEntityClass - this block + # can be removed when TempEntityClass is gone from apis_entities + # for now we at least check if the attribute exist + if hasattr(obj, "collection"): + if obj.collection.all().count() == 0: + col_name = getattr( + settings, "APIS_DEFAULT_COLLECTION", "manually created entity" + ) + col, created = Collection.objects.get_or_create(name=col_name) + obj.collection.add(col) return obj return GenericEntitiesForm