我正在开发一个使用 Django 和 South 进行迁移的项目。我想设置一些装置,用于在某些环境(开发、演示)中填充数据库,但不能在其他环境(生产)中填充数据库。例如,我希望系统中有一些数据,以便 UI 开发人员可以在他们正在处理的界面中使用一些数据,或者这样我们就可以快速为项目经理做一个演示,而无需通过手动设置管理界面。
虽然我找到了很多将自动化测试夹具与常规夹具分开的方法,但我还没有找到任何有关根据环境加载夹具的信息。这是否可能,或者人们是否有另一种方法来解决我忽略的这个问题?
I'm working on a project that is using Django and South for migrations. I would like to set up some fixtures that would be used to populate the database in some environments (development, demo) but not in others (production). For example, I would like there to be some data in the system so the UI developer has something to work with in the interface they are working on or so we can quickly do a demo for a project manager without having to manually set things up via the admin interface.
While I have found plenty of ways to separate automated testing fixtures from regular fixtures, I have not been able to find anything about loading fixtures based on environment. Is this possible, or is there another way people solve this problem I'm overlooking?
发布评论
评论(1)
对于
initial_data
灯具,您无能为力。然而,我一直觉得这些无论如何都没有达到最佳效用。您很少希望在每次调用syncdb
或migrate
时一次又一次地应用相同的固定装置。如果您使用一些不同名称的固定装置,则可以通过将以下内容添加到正向迁移中(来自 South docs)
所以实际上,您所需要的只是某种仅在某些环境中执行此操作的方法。对于开发人员来说,最简单的途径就是简单地依赖
DEBUG
。因此,前面的代码变为:如果您需要更好的控制,您可以创建某种在每个
local_settings.py
中不同的设置(或者您用于根据环境自定义设置的任何方法)。例如:There's not much you can do about
initial_data
fixtures. However, I've always felt those has less than optimal utility anyways. Rarely do you want the same fixture applied again and again with every call tosyncdb
ormigrate
.If you're using some differently named fixture, you can easily cause it to run with your migration by adding the following to your forwards migration (from the South docs)
So really, all you need is some way to only do that in certain environments. For dev, the easiest path would be to simply rely on
DEBUG
. So, the previous code becomes:If you need greater control, you can create some sort of setting that will be different in each
local_settings.py
(or whatever methodology you use to customize settings based on environment). For example: