我有两个简单的模型:
class Image(Model):
photo = models.CharField()
class Box(Model):
name = models.CharField()
image = models.ForeignKey(Image, blank=True, null=True)
当我想删除 Image 的对象时,Django 对 Box 模型进行选择查询:
>> Image.objects.all()[0].delete()
>> print len(connection.queries)
2
>> connection.queries
{u'time': u'0.000', u'sql': u'QUERY = u\'SELECT "box"."id", ... FROM "box" WHERE "image"."image_id" IN (%s)\' - PARAMS = (1,)'}
{u'time': u'0.000', u'sql': u'QUERY = u\'DELETE FROM "image" WHERE "id" IN (%s)\' - PARAMS = (1,)'}
Django 1.6b2
我已经尝试过on_delete=models.SET_NULL
,sqlite 和 PostgreSQL 也总是得到相同的结果。