I was trying to solve a simple problem with a site where a Person model has a married_to field as Foreign Key.
To maintain this when a user (PersonA) changes who he/she is married to the following should happen:
- The previous Person that PersonA was married to should set its married-field to None
- The new Person that PersonA is married to should update and set its married-field to PersonA (which in turn can trigger that the new Person's potentially previously married Person should set its married-field to None)
So what I tried was to override the save method something along the lines
if self.pk is not None and self.married is not None:
orig = Person.objects.get(pk=self.pk)
orig.married.married = None
orig.married.save()
if self.married is not None:
self.married.married = self
self.married.save()
super(Person, self).save()
I ran into maximum recursive problem etc. and started to search for answers but didnt find anythin conclusive.
What is the idiomatic way to do this for noobs like me...
Thanks