我只想在 django-reversion 中保存新的对象版本。我浏览了文档并没有找到任何关于它的信息。我怎样才能实现它?
问问题
876 次
1 回答
2
您可以使用该ignore_duplicates
选项。很遗憾
如果您真的想忽略跟随关系的重复项,您有两种可能性:
- 执行分叉并禁用限制
and explicit
在此处删除https://github.com/etianen/django-reversion/blob/master/reversion/revisions.py#L199
默认设置ignore_duplicates
为https://github.com/etianen/django-reversion/blob/master/reversion/revisions.py#L368True
小心,如上所述,它可能很慢。
- 监听修订后提交信号并手动删除重复版本
设置ignore_duplicates
为False
并添加信号接收器:
from django.db import transaction
from django.dispatch import receiver
from reversion.models import Revision, Version
from reversion.signals import post_revision_commit
def clear_versions(versions, revision):
count = 0
for version in versions:
previous_version = Version.objects.filter(
object_id=version.object_id,
content_type_id=version.content_type_id,
db=version.db,
id__lt=version.id,
).first()
if not previous_version:
continue
if previous_version._local_field_dict == version._local_field_dict:
version.delete()
count += 1
if len(versions_ids) == count:
revision.delete()
@receiver(post_revision_commit)
def post_revision_commit_receiver(sender, revision, versions, **kwargs):
transaction.on_commit(lambda: clear_versions(versions, revision))
它也可能很慢,但您可以异步执行(例如,在 Celery 任务中):
# tasks.py
@celery.task(time_limit=60, ignore_result=True)
def clear_versions(revision_id, versions_ids):
count = 0
if versions_ids:
for version in Version.objects.filter(id__in=versions_ids):
previous_version = Version.objects.filter(
object_id=version.object_id,
content_type_id=version.content_type_id,
db=version.db,
id__lt=version.id,
).first()
if not previous_version:
continue
if previous_version._local_field_dict == version._local_field_dict:
version.delete()
count += 1
if len(versions_ids) == count:
Revision.objects.only('id').get(id=revision_id).delete()
# signals.py
@receiver(post_revision_commit)
def post_revision_commit_receiver(sender, revision, versions, **kwargs):
transaction.on_commit(
lambda: clear_versions.delay(revision.id, [v.id for v in versions])
)
于 2018-05-17T11:34:30.540 回答