例如,我有一个Book模型,每本书都有其作者和标签。
def get_authors_first_tag(book):
try:
tag = book.author.tags.first()
except:
return None
else:
return tag.id
class Author(models.Model):
name = models.CharField(max_length=50)
tags = models.ManyToManyField('Tag')
class Book(models.Model):
name = models.CharField(max_length=50)
author = models.ForeignKey(Author)
tag = models.ForeignKey('Tag', null=True, on_delete=models.SET(get_authors_first_tag))
我想,在删除标签项时,书的标签将设置为其作者的第一个标签。这该怎么做?谢谢。 文档页面 - https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.SET
更新。 信号没有帮助
from django.db.models.signals import post_delete
from django.dispatch import receiver
class Book(models.Model):
# temporaryly set to NULL on delete,
# becouse for my neds I cannot use default behavior (models.CASCADE)
tag = models.ForeignKey('Tag', null=True, on_delete=models.SET_NULL)
@receiver(post_delete, sender=Tag)
def delete_tag(instance, **kwargs):
for book in instance.book_set.all():
try:
book.tag = book.author.tags.first()
book.save()
except:
pass
UPDATE2 最终工作解决方案是: (脏,但有效)
@receiver(post_delete, sender=Tag)
def delete_tag(instance, **kwargs):
books = Book.objects.all()
for book in books:
# after Tag item deleting, book.tag is set to None
# so if boook's tag is null, get it's author's first tag
if not book.tag:
book.tag = book.author.tags.first()
book.save()
答案 0 :(得分:2)
SET()
不接受任何参数,因此您应该使用signal接收器:
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()
将此代码放入models.py
或确保在启动时导入接收器的文件,以便接收器实际连接。