Django ForeignKey字段包含一个与行不对应的id,即使设置了ON DELETE CASCADE?

时间:2018-06-08 22:18:15

标签: python django

我正在处理一个包含模型Family的Django项目,该项目继承自BaseFamily,类似于

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import pre_save
from .timestamped_model import TimeStampedModel


class BaseFamily(TimeStampedModel):
    package = models.ForeignKey('lucy_web.Package',
                                models.SET_NULL,
                                blank=True,
                                null=True)
    lucy_guide = models.ForeignKey(
        User,
        blank=True,
        null=True,
        related_name='lucy_guide_%(class)s',
        limit_choices_to={'is_staff': True})


@receiver(pre_save)
def link_lucy_guide(sender, instance, **kwargs):
    """Assign company LucyGuide to family if not explicitly assigned."""
    if not issubclass(sender, BaseFamily):
        return

    if instance.company and not instance.lucy_guide:
        instance.lucy_guide = instance.company.lucy_guide

我们正在使用Django版本1.11.9;据我了解,如果未指定on_delete,则默认为models.CASCADE(参见the documentation)。

然而,在我目前的数据库中,我有一个家庭的实例(employee_first_name='Mark'employee_last_name='Flores'lucy_guide_id 687

enter image description here

但没有相应的auth_user

enter image description here

我的问题是:数据库是如何在这种状态下结束的?如果级联删除处于活动状态,family是否应与其lucy_guide一起删除?

0 个答案:

没有答案