如何使用django-reversion恢复相关对象

时间:2014-12-29 18:59:10

标签: django django-reversion

如何通过django-reversion恢复相关对象。

我尝试了以下不起作用......

models.py:

@reversion.register(follow=['bar_set',])
class Foo(models.Model):
    def save(self, *args, **kwargs):
        with transaction.atomic(), reversion.create_revision():
            return super(Foo, self).save(*args, **kwargs)

@reversion.register
class Bar(models.Model):
    foo = models.ForeignKey(Foo)

    def save(self, *args, **kwargs):
        with transaction.atomic(), reversion.create_revision():
            return super(Bar, self).save(*args, **kwargs)

以下测试

test.py:

class TestModels(TestCase):
    def test_soft_delete(self):
        from myapp.models import Foo, Bar
        a = Foo()
        a.save()

        b1 = Bar(foo=a)
        b1.save()
        b2 = Bar(foo=a)
        b2.save()

        a.delete()

        self.assertEqual(0, Foo.objects.count())
        self.assertEqual(0, Bar.objects.count())

        version = reversion.get_deleted(Foo)[0]  # There is only one.
        version.revert()

        self.assertEqual(1, Foo.objects.count())
        self.assertEqual(2, Bar.objects.count()) # HERE IT FAILS: 2 != 0

1 个答案:

答案 0 :(得分:0)

最后,我能够以一些次优的方式手动完成:

我已将代码放入自定义QuerySet

class FooQuerySet(models.QuerySet):
    def restore(self, id):
        # restore
        deleted_list = reversion.get_deleted(self.model)
        deleted_list.get(id=id).revision.revert()

        # restore related 
        deleted_list = reversion.get_deleted(Bar)
        bar_set = filter(lambda s: s.field_dict['foo'] == id, deleted_list)

        for bar in bar_set:
            bar.revision.revert()

然后Foo对象需要一个自定义管理器

class Foo(models.Model):
    ...
    objects = FooQuerySet.as_manager()

可以从模型中自动提取相关字段。但是手动完成它对我的目的来说已经足够了。