Django 1.3 和南迁移
我有一个现有的项目,它广泛使用南迁移将数据加载到其表中。
自从升级到 Django 1.3 以来,我们的单元测试不再运行,因为它们找不到它们所依赖的数据。
- 此行为是否是由于 1.3 中向后不兼容的更改之一
- 有没有一种简单的方法可以让我将所有这些迁移转换为固定装置?
I have an existing project which extensively uses South migrations to load data into its tables.
Since upgrading to Django 1.3 our unit tests no longer run because they cannot find the data they rely on.
- Is this behaviour is due to one of the backwards incompatible changes in 1.3
- Is there an easy way for me to convert all these migrations into fixtures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,此行为是由于此更改造成的。
南主干似乎有一个解决方法(请参阅https://bitbucket.org/andrewgodwin /south/changeset/21a635231327 )所以你可以尝试South开发版本(它在我的中相当稳定)经验)。
您可以尝试在设置中更改数据库名称(为了获得干净的环境),运行
./manage.pysyncdb
和./manage.py migrate
然后执行./manage.py dumpdata
Yes, this behavior is due to this change.
There seems to be a workaround in South trunk (see https://bitbucket.org/andrewgodwin/south/changeset/21a635231327 ) so you can try South development version (it is quite stable in my experience).
You may try to change the DB name in settings (in order to get clean environment), run
./manage.py syncdb
and./manage.py migrate
and then do./manage.py dumpdata
我今天遇到了这个问题。最终我最终重构了我的迁移,以便它们使用辅助函数来实际插入数据,然后从我的测试的 setUp() 中调用相同的函数。
一些提示;
使您的辅助函数将模型类作为参数,以便您可以在迁移中使用 orm['yourapp.YourModel'] 以及在测试中使用 models.YourModel 来调用它们。这也显示了主要限制:South 适用于从那时起架构已更改的模型,测试代码无法做到这一点。我很幸运,这个特定的模型没有改变。
如果您想将辅助方法保留在迁移中,您会发现无法直接导入 yourapp.migrations.0001_some_migration,因为标识符不能以数字开头。使用类似
migration_0001 = importlib.import_module('yourapp.migrations.0001_some_migration')
而不是 import 语句。I hit this issue today. Eventually I ended up refactoring my migrations so that they use helper functions to actually insert the data, and then calling the same functions from the setUp() of my tests.
Some hints;
Make your helper functions take the model class as an argument, so you can call them with orm['yourapp.YourModel'] from the migration and with models.YourModel from the test. That also shows the main limitation: South works for models whose schema has changed since then, the test code can't do that. I was lucky in that this particular model hasn't changed.
If you want to keep the helper methods inside the migrations, you'll find that you can't directly import yourapp.migrations.0001_some_migration because identifiers can't start with numbers. Use something like
migration_0001 = importlib.import_module('yourapp.migrations.0001_some_migration')
instead of an import statement.