If a user is de-activated I want all memberships deleted. I thought this code in models.py would do it. But is doesn't, the relation is still there.
class User(AbstractUser):
membership_type = ManyToManyField(MembershipType, blank=True)
....
def __init__(self, *args, **kwargs):
super(User, self).__init__(*args, **kwargs)
self.old_is_active = self.is_active
def save(self, **kwargs):
# Membership is cancelled
if self.old_is_active is True and self.is_active is False:
self.membership_type.clear()
super(User, self).save()
I also changed it and debugged it like this.
@receiver(post_save, sender=User)
def clear_membership(sender, instance, **kwargs):
if instance.old_is_active is True and instance.is_active is False:
print('clearing post_save')
print(instance.membership_type.all())
instance.membership_type.clear()
print(instance.membership_type.all())
It prints out the relation.
And after the clear()
the relations are gone.
But when I look in my Admin GUI, the relation is still there ...?