Skip to content

Instantly share code, notes, and snippets.

@Tobi-De
Last active September 15, 2020 11:39
Show Gist options
  • Save Tobi-De/b83b12208f617da1b386a41acc69ec92 to your computer and use it in GitHub Desktop.
Save Tobi-De/b83b12208f617da1b386a41acc69ec92 to your computer and use it in GitHub Desktop.
class Registration(TimeStampedModel):
....
tutor_m = models.ForeignKey(
"universities.Tutor",
on_delete=models.CASCADE,
)
....
from django.db.models.signals import post_delete
from django.dispatch import receiver
from .models import Registration
# TODO when deleting multiple registration with same tutor, an error occurs
@receiver(post_delete, sender=Registration)
def registration_delete(sender, instance, *args, **kwargs):
if not (Registration.objects.filter(tutor_m=instance.tutor_m).exists():
try:
instance.tutor_m.delete()
except Tutor.DoesNotExist:
pass
# if len(Registration.objects.filter(tutor_m=instance.tutor_m)) == 1:
# instance.tutor_m.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment