数据迁移期间不可用的模型

时间:2014-03-28 10:47:00

标签: django django-south data-migration django-reversion

我在我的应用中添加了Target模型,该模型将Revision对象(来自django-reversion)标记为一对一字段。标记取决于版本集中的对象,如果这些对象中的任何一个需要标记,则应为整个修订设置标记。我正在尝试使用django-south数据迁移来遍历数据库中的所有Revision个对象,检查关联的version_set中的每个对象,并在必要时设置标记。

class Migration(DataMigration):

    def forwards(self, orm):
        for revision in orm["reversion.Revision"].objects.all():
            try:
                revision.target # the revision is already tagged
            except: # revision.target raises different sorts of DoesNotExist errors, 
                    # and I can't work out how to catch all of them
                for version in revision.version_set.all():
                    try:
                        tag = version.object.get_tag() # all models in my app that   
                        orm.Target.objects.create(     # specify tags have this method
                                revision=revision, 
                                tag=tag)
                        break
                    except AttributeError: # the version set doesn't contain any
                        pass               # models that specify a tag

给出错误:

    [... lots of stuff cut ...], line 21, in forwards
    for version in revision.version_set.all():
AttributeError: 'Revision' object has no attribute 'version_set'

如何在其他应用中的数据迁移期间访问version_setRevision对象?

编辑感谢Daniel,我还在继续。在以下代码中,我尝试通过Migration自己的orm

访问所有模型
def forwards(self, orm):
    my_models = { # models with a .get_tag() method
            "modela": orm.ModelA,
            "modelb": orm.ModelB,
            }
    for revision in orm["reversion.Revision"].objects.all():
        for version in orm["reversion.Version"].objects.filter(revision=revision):
            try:
                model = my_models[version.content_type.model]
                instance = model.objects.get(id=version.object_id)
                tag = instance.get_tag()
                orm.RevisionTargetLanguage.objects.create(
                        revision=revision,
                        tag=tag)
                break
            except KeyError: # not one of the models with a .get_tag() method
                pass

instance = model.objects.get(id=version.object_id_int)行失败,DoesNotExist: ModelB matching query does not exist例外。

1 个答案:

答案 0 :(得分:1)

你可以尝试

orm['reversion.Version'].objects.filter(revision=revision)