我正在尝试修改此文件https://github.com/alex/django-taggit/blob/master/taggit/models.py以便当标签不与任何其他对象关联时,它会被删除。
这是我添加到文件末尾的内容:
# ---
# Delete the tags that are not used by any other object
from django.db.models.signals import post_delete
def after_deleting(sender, instance, **kwargs):
if TaggedItem.objects.filter(tag=instance.tag_id).count() == 0:
print "Deleting tag", instance
t = Tag.objects.get(pk=instance.tag_id)
t.delete()
post_delete.connect(after_deleting, sender=TaggedItem)
它没有按预期工作。当我运行它时,会出现此错误:
Exception Type: DoesNotExist
Exception Value: Tag matching query does not exist.
您的帮助将不胜感激。