如何通过 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