我意识到如果我通过filter().delete()
删除项目,模型上的object_set不会更新(这没关系)但是我想在删除后更新_set
以便通过:
def test_prefetch(self):
game = Game.objects.prefetch_related('bubble_set').get()
game.fill_board()
self.assertEqual(game.bubble_set.count(), game.width * game.height)
game.bubble_set.filter(x=5).delete()
# Fails
self.assertNotEqual(game.bubble_set.count(), game.width * game.height)
game = Game.objects.prefetch_related('bubble_set').get()
答案 0 :(得分:0)
您需要重新获取该对象。因此可以在game = Game.objects.prefetch_related('bubble_set').get()
之后.delete()
进行。{/ p>