From 722f562503390e227d7a4138794db2afaaab6908 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 17 Jul 2025 16:31:50 +0200 Subject: 🐛 relations forms - clean duplicates, fix deletion (refs #6399) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/models.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'ishtar_common/models.py') diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 11ac75876..b7d6004c3 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -675,6 +675,21 @@ class RelationItem(models.Model): full=full, ) + def clean_relations(self): + # clean duplicates + for side in ("right", "left"): + currents = [] + rels = getattr(self, f"{side}_relations") + for relation in list(rels.all()): + key = (getattr(relation, f"{side}_record_id"), + relation.relation_type_id) + if key not in currents: + currents.append(key) + else: + relation._no_cascade_delete = True + # prevent cascade delete + relation.delete() + class JsonDataSectionManager(models.Manager): def get_by_natural_key(self, name, app_label, model): @@ -869,6 +884,9 @@ class GeneralRecordRelations(Imported): def post_delete_record_relation(sender, instance, **kwargs): + if getattr(instance, "_no_cascade_delete", False): + # do not delete on "clean_relations" call + return # delete symmetrical or inverse relation sym_rel_type = instance.relation_type if not instance.relation_type.symmetrical: -- cgit v1.2.3