如何在Django中进行破坏性操作后刷新foreignkey_set?

时间:2012-07-16 06:18:52

标签: django

我意识到如果我通过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()

1 个答案:

答案 0 :(得分:0)

您需要重新获取该对象。因此可以在game = Game.objects.prefetch_related('bubble_set').get()之后.delete()进行。{/ p>