如何正确地在Django南移动装载装置? [英] How to load fixtures in Django south migrations properly?
问题描述
ff.User
(我的自定义用户模型): def forward(self,orm):
from django.core.management import call_command
fixture_path =/absolute/path/to/my/fixture/load_initial_users.json
call_command (loaddata,fixture_path)
所有的一直运行良好,直到我添加了另一个字段到我的 ff.User
模型,进一步向下移动线。我的装载装载现在中断了:
DatabaseError:安装夹具的问题C:\<编辑> create_users.json':
无法加载ff.User(pk = 1):(1054,字段列表中的未知列'timezone_id')
时区是我添加到我的用户模型的字段(ForeignKey)。
ff.User与数据库,所以Django ORM放弃了DB错误。不幸的是,我不能在我的装置中指定我的模型为 orm ['ff.User']
,这似乎是南方做事的方式。
我应该如何使用南方正确地加载固定装置,以便一旦这些固定装置的模型被修改就不会中断?
阅读以下两篇文章有助于我提出一个解决方案:
http://andrewingram.net/2012/dec/common-pitfalls-django-south/#要小心的装置
http://news.ycombinator.com/item?id=4872596
具体来说,我重写了我的数据迁移,以使用'dumpscript'
我需要修改生成的脚本有点与南方合作。而不是从ff.models import
用户
我做
User = orm ['ff.User']
这个工作与我想要的一样。此外,它具有不像硬件编码的ID那样的好处。
I am using Django 1.5b1 and south migrations and life has generally been great. I have some schema updates which create my database, with a User table among others. I then load a fixture for ff.User
(my custom user model):
def forwards(self, orm):
from django.core.management import call_command
fixture_path = "/absolute/path/to/my/fixture/load_initial_users.json"
call_command("loaddata", fixture_path)
All has been working great until I have added another field to my ff.User
model, much further down the migration line. My fixture load now breaks:
DatabaseError: Problem installing fixture 'C:\<redacted>create_users.json':
Could not load ff.User(pk=1): (1054, "Unknown column 'timezone_id' in 'field list'")
Timezone is the field (ForeignKey) which I added to my user model.
The ff.User differs from what is in the database, so the Django ORM gives up with a DB error. Unfortunately, I cannot specify my model in my fixture as orm['ff.User']
, which seems to be the south way of doing things.
How should I load fixtures properly using south so that they do not break once the models for which these fixtures are for gets modified?
Reading the following two posts has helped me come up with a solution:
http://andrewingram.net/2012/dec/common-pitfalls-django-south/#be-careful-with-fixtures
http://news.ycombinator.com/item?id=4872596
Specifically, I rewrote my data migrations to use output from 'dumpscript'
I needed to modify the resulting script a bit to work with south. Instead of doing
from ff.models import User
I do
User = orm['ff.User']
This works exactly like I wanted it to. Additionally, it has the benefit of not hard-coding IDs, like fixtures require.
这篇关于如何正确地在Django南移动装载装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!