2

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

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

模型.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)

以及以下测试

测试.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
4

1 回答 1

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()

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

于 2015-01-02T14:01:46.103 回答