diff options
Diffstat (limited to 'archaeological_context_records/models.py')
-rw-r--r-- | archaeological_context_records/models.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index a8c6a1dcc..9a4fa7880 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -19,6 +19,7 @@ from django.conf import settings from django.contrib.gis.db import models +from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _, ugettext, pgettext @@ -252,6 +253,24 @@ class RelationType(GeneralType): verbose_name_plural = _(u"Relation types") ordering = ('order',) + def clean(self): + # cannot have symmetrical and an inverse_relation + if self.symmetrical and self.inverse_relation: + raise ValidationError( + _(u"Cannot have symmetrical and an inverse_relation")) + + def save(self, *args, **kwargs): + obj = super(RelationType, self).save(*args, **kwargs) + # after saving check that the inverse_relation of the inverse_relation + # point to the saved object + if self.inverse_relation \ + and (not self.inverse_relation.inverse_relation + or self.inverse_relation.inverse_relation != self): + self.inverse_relation.inverse_relation = self + self.inverse_relation.symmetrical = False + self.inverse_relation.save() + return obj + class RecordRelations(models.Model): left_record = models.ForeignKey(ContextRecord, related_name='left_relations') |