Django未删除相关对象

时间:2018-08-05 22:00:49

标签: python-3.x django-models

我有问题,我的模型定义如下:

class Customer(models.Model):
    first_name = models.CharField(max_length=40, verbose_name="First Name")
    last_name = models.CharField(max_length=40, verbose_name="Last Name")
    gender = models.CharField(max_length=1, choices=(('0', 'Female'), ('1','Male')))
    doc_id = models.CharField(max_length=20, verbose_name="Document ID")
    category = models.ForeignKey(BlackList, on_delete=models.SET_NULL, related_name="customers", verbose_name="Category", null=True)
    def __str__(self): return ' '.join([self.first_name, self.last_name])

class PhoneNumber(models.Model):
    PhoneValidator = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    number = models.CharField(max_length=20, verbose_name = "Phone Number", validators=[PhoneValidator])
    costumer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True, related_name="phones")

    def __str__(self): return self.number

但是问题是,当我删除电话号码时,相关的“客户”对象也被删除了,如何避免这种情况?

0 个答案:

没有答案