级联删除不能正常工作

时间:2013-07-09 13:20:39

标签: django django-models

如果我要删除联系人对象,则相关的Unsubscribe对象应该级联删除。看来情况并非如此,为什么?以下看到的Contact模型有什么问题吗?谢谢。

取消

class Unsubscribe(models.Model):
    """

    Notes:
    See: http://www.screamingatmyscreen.com/2012/6/django-and-generic-relations/
    """
    content_type = models.ForeignKey(ContentType, help_text="Represents the name of the model")
    object_id = models.PositiveIntegerField(help_text="stores the object id")
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    reason = models.CharField(max_length=60)

    request_made = models.DateTimeField(auto_now_add=True,
                                   help_text="Shows when object was created.")

class Contact(models.Model):
    first_name = models.CharField(max_length=60, blank=True)
    last_name = models.CharField(max_length=60, blank=True)
    created = models.DateTimeField(auto_now_add=True, help_text="Shows when object was created.")

    objects = ContactManager()

    #FK
    group = models.ForeignKey(Group, related_name='contacts', blank=True, null=True)
    contact_owner = models.ForeignKey(User)

1 个答案:

答案 0 :(得分:2)

ForeignKeyContact之间没有直接的Unsubscribe关系。

Unsubscribe模型中,通用关系引用Contact对象(这仅转换为存储object_idcontent_type_id的数据库表条目 - 不是实际的外键引用),因此级联删除不起作用。在Unsubscribe对象的pre_delete信号中删除相应Contact对象的一种方法是