I tried to update a Transaction model queryset in the below pre_delete signal that connected to the Wallet model, but it didn't work.
The signal triggers, and all lines work correctly, except the last line :(
@receiver(pre_delete, sender=Wallet)
def delete_wallet(sender, instance, **kwargs):
if instance.is_default:
raise ValidationError({"is_default": "You can't delete the default wallet"})
q = {"company": instance.company} if instance.company else {"user": instance.user}
try:
default_wallet = Wallet.objects.get(**q, is_default=True)
except Wallet.DoesNotExist:
default_wallet = Wallet.objects.filter(**q).exclude(pk=instance.pk).first()
if not default_wallet:
raise ValidationError({"is_default": "You can't delete the last wallet"})
instance.transactions.all().update(wallet=default_wallet)