From d0c41db15d9bc583a639a6f46b9376092dda1e11 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 6 Sep 2019 10:57:54 +0200 Subject: Serialization: natural key for relations between context records --- archaeological_context_records/models.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'archaeological_context_records/models.py') diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index f0786e762..875a48019 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -884,6 +884,13 @@ class RelationType(GeneralRelationType): ordering = ('order', 'label') +class RecordRelationsManager(models.Manager): + def get_by_natural_key(self, left_record, right_record, relation_type): + return self.get(left_record__uuid=left_record, + right_record__uuid=right_record, + relation_type__txt_idx=relation_type) + + class RecordRelations(GeneralRecordRelations, models.Model): MAIN_ATTR = 'left_record' left_record = models.ForeignKey(ContextRecord, @@ -891,6 +898,7 @@ class RecordRelations(GeneralRecordRelations, models.Model): right_record = models.ForeignKey(ContextRecord, related_name='left_relations') relation_type = models.ForeignKey(RelationType) + objects = RecordRelationsManager() TABLE_COLS = [ "left_record__label", "left_record__unit", "left_record__parcel", "relation_type", @@ -922,6 +930,10 @@ class RecordRelations(GeneralRecordRelations, models.Model): ("view_recordrelation", "Can view all Context record relations"), ] + def natural_key(self): + return self.left_record.uuid, self.right_record.uuid, \ + self.relation_type.txt_idx + post_delete.connect(post_delete_record_relation, sender=RecordRelations) -- cgit v1.2.3