For example, I have a Book model, each book has its author and tag.
def get_authors_first_tag(book):
try:
tag = book.author.tags.first()
except:
The SET()
doesn't accept any arguments, so you should use the signal receiver:
from django.db.models.signals.pre_delete
from django.dispatch import receiver
@receiver(pre_delete, sender=Tag)
def reset_tag(sender, **kwargs):
tag = kwargs['instance']
for book in books.filter(tag=tag):
book.tag = book.author.tags.first()
book.save()
Put this code into your models.py
or make sure the file you put the receiver into is imported at the startup, so that the receiver is actually connected.