Last active
September 15, 2020 11:39
-
-
Save Tobi-De/b83b12208f617da1b386a41acc69ec92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Registration(TimeStampedModel): | |
.... | |
tutor_m = models.ForeignKey( | |
"universities.Tutor", | |
on_delete=models.CASCADE, | |
) | |
.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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