django在向后迁移之后从fixture装载数据/ loaddata正在使用模型模式而不是数据库模式 [英] django loading data from fixture after backward migration / loaddata is using model schema not database schema

查看:133
本文介绍了django在向后迁移之后从fixture装载数据/ loaddata正在使用模型模式而不是数据库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入的旧数据比我当前的模型架构有一个问题。我使用并导致错误的流程:

I have recenty came across a problem while importing older data than my current model schema. Flow which I use and lead to error:


  • dumpdata with python manage.py dumpdata - > 0002

  • 对模型进行一些修改

  • 使用python manage.py schemamigration app_name生成迁移--auto - > 0003

  • 运行迁移

  • 使用数据库

  • 迁移到0002

  • loaddata生成我有当前(迁移0003)字段的SQL,导致失败的loaddata进程(mpoly被添加字段)

  • dumpdata with python manage.py dumpdata -> 0002
  • make some modifications to model
  • generate migration with python manage.py schemamigration app_name --auto -> 0003
  • run migration
  • play with database
  • migrate to 0002
  • loaddata generate SQL in which I have current (migration 0003) fields, and cause failing loaddata process (mpoly is added field)

  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py",

第44行,执行
return self.cursor.execute(query,args)
DatabaseError:关系local_province的mpoly列没有
不存在
LINE 1:... e(id,name,slug,mpoly)V ...

line 44, in execute return self.cursor.execute(query, args) DatabaseError: column "mpoly" of relation "localization_province" does not exist LINE 1: ...e" ("id", "name", "slug", "mpoly") V...




  • 在models.py中注释更改完成在0003之前,使所有工作正常

  • 有没有办法避免在向后迁移后玩模型,如果我想加载数据?

    也许我错过了一些非常明显的东西...

    Maybe I'm missing something really obvious...

    PS: m使用South 7.3,Django 1.2.3和PostgreSQL 8.4作为数据库后端。

    PS: I'm using South 7.3, Django 1.2.3 and PostgreSQL 8.4 as database backend.

    推荐答案

    Alex Vidal 为我们提供了一个很好的快速修复,当我们在我们的工作。它需要Gary Bernhardt的 Dingus 图书馆。一旦我们有时间,我们将考虑到丁丁依赖性,并向南方提交拉动请求,但如果您现在处于绑定状态,可能会让您失望:

    Alex Vidal proposed a nice quick fix for this when it bit us at our job. It requires Gary Bernhardt's Dingus library. Once we have time we'll factor out the Dingus dependency and submit a pull request to South, but if you're in a bind right now this may get you out of it:

    from dingus import patch
    
    
    def loaddata(orm, fixture_name):
        _get_model = lambda model_identifier: orm[model_identifier]
    
        with patch('django.core.serializers.python._get_model', _get_model):
            from django.core.management import call_command
            call_command("loaddata", fixture_name)
    

    用法:

    from apps.common.utils import loaddata
    
    
    class Migration(DataMigration):
        def forwards(self, orm):
            loaddata(orm, "initial_fjords.json")
    

    到目前为止,我们仅在Django 1.3中测试过。 编辑:我检查了Django的 _get_model 历史记录,这可以与Django 0.95及以上版本配合使用。

    We've tested only in Django 1.3 so far. Edit: I checked Django's _get_model history and this should work with Django 0.95 and up.

    这篇关于django在向后迁移之后从fixture装载数据/ loaddata正在使用模型模式而不是数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆