Django failing to cascade-delete related generic foreign key object
我在我的模型中定义了以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class TaskLink(models.Model): task = model.ForeignKey(Task) link_choices = ( models.Q(app_label="accounts", model="location"), # Other models are also linked to here. ) linked_content_type = \ models.ForeignKey( ContentType, limit_choices_to=link_choices ) linked_object_id = models.PositiveIntegerField() linked_object = \ generic.GenericForeignKey( 'linked_object_content_type', 'linked_object_id' ) |
这个模型将
当删除一个
1 | django.core.exceptions.FieldError: Cannot resolve keyword 'object_id' into field. Choices are: id, linked_object, linked_object_content_type, linked_object_content_type_id, linked_object_id, task, task_id |
该视图是
我错过了什么?
编辑:这似乎是Django中的一个bug;当通过通用外键级联删除对象时,Django忽略了您传递给
我已经通过django邮件列表发送了这个问题,因为这种行为可能是有意的。
重命名tasklink的字段名
1 2 | linked_content_type >>> content_type linked_object_id >>> object_id |
或者在删除"location"对象时写入预信号删除链接对象"tasklink"
1 2 3 4 5 6 7 8 | from django.db.models.signals import pre_delete from django.dispatch import receiver @receiver(pre_delete, sender=Location, dispatch_uid='location_delete_signal') def deleted_gfk_TaskLink(sender, instance, using, **kwargs): ctype = ContentType.objects.get_for_model(sender) obj = TaskLink.objects.get(linked_content_type=ctype, linked_object_id=instance.id) obj.delete() |
自定义信号参考:https://micropyramid.com/blog/using-djangos-built-in-signals-and-writing-custom-signals/