目录
-- 禁用外键约束
SET FOREIGN_KEY_CHECKS=0;
-- 清空表格
truncate table app1_book;
truncate table app1_publish;
INSERT INTO `app1_publish` VALUES (1, '张三', '昆明', '[email protected]');
INSERT INTO `app1_publish` VALUES (2, '李四', '玉溪', '[email protected]');
INSERT INTO `app1_publish` VALUES (3, '王五', '大理', '[email protected]');
INSERT INTO `app1_book` VALUES (1, '标题1', 1.00, '2023-08-09', 1);
INSERT INTO `app1_book` VALUES (2, '标题2', 1.00, '2023-08-09', 1);
INSERT INTO `app1_book` VALUES (3, '标题3', 1.00, '2023-08-09', 1);
INSERT INTO `app1_book` VALUES (4, '标题三', 1.00, '2023-08-09', 2);
INSERT INTO `app1_book` VALUES (5, '标题四', 1.00, '2023-08-09', 2);
-- 启动外键约束
SET FOREIGN_KEY_CHECKS=1;
一、CASCADE
这就是默认的选项,级联删除,你无需显性指定它。
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5, decimal_places=2)
pub_date = models.DateField()
publish = models.ForeignKey("Publish", on_delete=models.CASCADE)
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=64)
email = models.EmailField()
def delete(request):
delete = Publish.objects.filter(pk=1).delete()
print(delete)
return JsonResponse({"msg":"删除成功","data":delete})
注意:
使用CASCADE会将关联的其他内容页进行删除,如删除 Publish 中pk=1的会把Book中publish__id=1的也一起删除
二、PROTECT
保护模式,如果采用该选项,删除的时候,会抛出ProtectedError
错误。
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5, decimal_places=2)
pub_date = models.DateField()
publish = models.ForeignKey("Publish", on_delete=models.PROTECT)
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=64)
email = models.EmailField()
def delete(request):
try:
delete = Publish.objects.filter(pk=1).delete()
print(delete)
return JsonResponse({"msg":"删除成功","data":delete})
except Exception as e:
print(e)
return JsonResponse({"msg": "删除失败", "error": str(e)})
删除无引用的外键数据
def delete(request):
try:
delete = Publish.objects.filter(pk=1).delete()
print(delete)
return JsonResponse({"msg":"删除成功","data":delete})
except Exception as e:
print(e)
exception_name = type(e).__name__
print('exception_name:',exception_name)
return JsonResponse({"msg": "删除失败", "error": str(e)})
三、SET_NULL
置空模式,删除的时候,外键字段被设置为空,前提就是blank=True, null=True
,定义该字段的时候,允许为空。
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5, decimal_places=2)
pub_date = models.DateField()
publish = models.ForeignKey("Publish", on_delete=models.SET_NULL,null=True)
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=64)
email = models.EmailField()
def delete(request):
try:
delete = Publish.objects.filter(pk=1).delete()
print(delete)
return JsonResponse({"msg":"删除成功","data":delete})
except Exception as e:
print(e)
exception_name = type(e).__name__
print('exception_name:',exception_name)
return JsonResponse({"msg": "删除失败", "error": str(e)})
四、SET_DEFAULT
置默认值,删除的时候,外键字段设置为默认值,所以定义外键的时候注意加上一个默认值。
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5, decimal_places=2)
pub_date = models.DateField()
publish = models.ForeignKey("Publish", on_delete=models.SET_DEFAULT,default="",null=True)
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=64)
email = models.EmailField()
def delete(request):
try:
delete = Publish.objects.filter(pk=1).delete()
print(delete)
return JsonResponse({"msg":"删除成功","data":delete})
except Exception as e:
print(e)
exception_name = type(e).__name__
print('exception_name:',exception_name)
return JsonResponse({"msg": "删除失败", "error": str(e)})
五、
SET(值)
自定义一个值,该值当然只能是对应的实体了
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5, decimal_places=2)
pub_date = models.DateField()
publish = models.ForeignKey("Publish", on_delete=models.SET(2))
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=64)
email = models.EmailField()
def delete(request):
try:
delete = Publish.objects.filter(pk=1).delete()
print(delete)
return JsonResponse({"msg":"删除成功","data":delete})
except Exception as e:
print(e)
exception_name = type(e).__name__
print('exception_name:',exception_name)
return JsonResponse({"msg": "删除失败", "error": str(e)})
总结
1、需求:
如果需要删除引用的外键后才能删除就使用: on_delete=models.PROTECT
如果需删除全部使用: on_delete=models.CASCADE
允许删除字段: on_delete=models.SET_NULL 或者
on_delete=models.SET_DEFAULT