gpt4 book ai didi

迁移后 Django 还原无法恢复对象

转载 作者:行者123 更新时间:2023-12-05 04:10:39 24 4
gpt4 key购买 nike

我有这个模型:

from django.contrib.auth.models import User
from django.db import models
import reversion


@reversion.register()
class BlogPost(models.Model):
author = models.ForeignKey(User)
title = models.CharField(max_length=32)
content = models.TextField()

现在我决定将这个字段添加到模型中:

random_field = models.PositiveIntegerField(null=False, blank=False)

我创建了迁移并选择了默认值:

operations = [
migrations.AddField(
model_name='blogpost',
name='random_field',
field=models.PositiveIntegerField(default=10),
preserve_default=False,
),
]

并迁移它。

现在,我正在使用具有恢复支持的 Django 管理,我在迁移之前修改了几次博客文章,现在我想迁移到没有随机字段的版本。它说:

Could not save BlogPost object version - missing dependency.

有什么办法可以避免这种情况吗?我认为这是因为迁移没有创建修订。似乎错误就在这里: reversion/models.py#L21

我正在使用

Django==1.11.1
django-reversion==2.0.8

使用 sqlite 数据库。

有什么办法可以避免这种情况吗?

最佳答案

根据 GH issue :

Ah, I see. annoying. It appears that the revisions can handle deleting a field, but not adding one.

Automating this is pretty much impossible. There are revision databases out there with gigbytes of old revisions, and updating old revision data when a new migration occurs could take days of runtime per migration. It's not really feasible.

My general approach is to delete the revision data after a migration. If you really want to keep it, then you can write your own data migration the in the app.

-- 项目作者。

因此,似乎根本不支持迁移后恢复,虽然它有时可能会起作用,但并不意味着以这种方式使用。

关于迁移后 Django 还原无法恢复对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44124100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com